1
# Copyright 2012, 2013 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
from __future__ import print_function
9
from pyelasticsearch.exceptions import ElasticHttpNotFoundError
10
from charmworld.utils import get_ini
11
from charmworld.models import (
17
from charmworld.search import ElasticSearchClient
20
def main(argv, stderr=sys.stderr, db=None, index_client=None):
21
"""The main function for the server start timestamp removal script."""
24
'Please provide the ID of the charm you wish to remove.',
32
connection = getconnection(settings)
33
db = getdb(connection, settings.get('mongo.database'))
35
# If the specified charm does not exist, tell the user.
36
charm_query = {'_id': charm_id}
37
charm_data = db.charms.find_one(charm_query)
38
if charm_data is None:
39
print('Unknown charm ID: %r' % charm_id, file=stderr)
42
if index_client is None:
43
index_client = ElasticSearchClient.from_settings(settings)
45
# The charm needs to be removed from the search engine.
47
index_client.delete_charm(charm_data)
48
except ElasticHttpNotFoundError:
49
# It is fine if the charm was not indexed.
52
# If the charm is featured, we want to unfeature it.
53
source = FeaturedSource.from_db(db)
54
source.clear_featured(charm_data, 'charm')
56
# Finally, remove all the revisions of the charm document from the
58
charm_query = CharmSource.make_all_revisions_query(charm_data)
59
db.charms.remove(charm_query, justOne=True)
64
"""Script entry point for removing a server's start up timestamp."""
65
sys.exit(main(sys.argv))