OLD | NEW |
1 # Copyright 2012, 2013 Canonical Ltd. This software is licensed under the | 1 # Copyright 2012, 2013 Canonical Ltd. This software is licensed under the |
2 # GNU Affero General Public License version 3 (see the file LICENSE). | 2 # GNU Affero General Public License version 3 (see the file LICENSE). |
3 | 3 |
4 from __future__ import print_function | 4 from __future__ import print_function |
5 | 5 |
6 __metaclass__ = type | 6 __metaclass__ = type |
7 | 7 |
8 import sys | 8 import sys |
9 from pyelasticsearch.exceptions import ElasticHttpNotFoundError | 9 from charmworld.utils import ( |
10 from charmworld.utils import get_ini | 10 delete_charm, |
| 11 get_ini, |
| 12 ) |
11 from charmworld.models import ( | 13 from charmworld.models import ( |
12 CharmSource, | |
13 FeaturedSource, | |
14 getconnection, | 14 getconnection, |
15 getdb, | 15 getdb, |
16 ) | 16 ) |
17 from charmworld.search import ElasticSearchClient | 17 from charmworld.search import ElasticSearchClient |
18 | 18 |
19 | 19 |
20 CHARM_ID_MESSAGE = ( | 20 CHARM_ID_MESSAGE = ( |
21 'Please provide a charm ID, including revision. E.g., ' | 21 'Please provide a charm ID, including revision, e.g., ' |
22 '~charmers/precise/apache2/14') | 22 '~charmers/precise/apache2/14') |
23 | 23 |
24 | 24 |
25 def main(argv, stderr=sys.stderr, db=None, index_client=None): | 25 def main(argv, stderr=sys.stderr, db=None, index_client=None): |
26 """The main function for the server start timestamp removal script.""" | 26 """The main function for the charm removal script.""" |
27 if len(argv) != 2: | 27 if len(argv) != 2: |
28 print( | 28 print( |
29 'Please provide the ID of the charm you wish to remove.', | 29 'Please provide the ID of the charm you wish to remove.', |
30 file=stderr) | 30 file=stderr) |
31 return 1 | 31 return 1 |
32 | 32 |
33 charm_id = argv[1] | 33 charm_id = argv[1] |
34 | 34 |
35 if db is None: | 35 if db is None: |
36 settings = get_ini() | 36 settings = get_ini() |
37 connection = getconnection(settings) | 37 connection = getconnection(settings) |
38 db = getdb(connection, settings.get('mongo.database')) | 38 db = getdb(connection, settings.get('mongo.database')) |
39 | 39 |
40 # If the specified charm does not exist, tell the user. | 40 # If the specified charm does not exist, tell the user. |
41 charm_query = {'_id': charm_id} | 41 charm_query = {'_id': charm_id} |
42 charm_data = db.charms.find_one(charm_query) | 42 charm_data = db.charms.find_one(charm_query) |
43 if charm_data is None: | 43 if charm_data is None: |
44 print('Unknown charm ID: %r' % charm_id, file=stderr) | 44 print('Unknown charm ID: %r' % charm_id, file=stderr) |
45 print(CHARM_ID_MESSAGE, file=stderr) | 45 print(CHARM_ID_MESSAGE, file=stderr) |
46 return 1 | 46 return 1 |
47 | 47 |
48 if index_client is None: | 48 if index_client is None: |
49 index_client = ElasticSearchClient.from_settings(settings) | 49 index_client = ElasticSearchClient.from_settings(settings) |
50 | 50 |
51 # The charm needs to be removed from the search engine. | 51 delete_charm(charm_data, db, index_client) |
52 try: | |
53 index_client.delete_charm(charm_data) | |
54 except ElasticHttpNotFoundError: | |
55 # It is fine if the charm was not indexed. | |
56 pass | |
57 | |
58 # If the charm is featured, we want to unfeature it. | |
59 source = FeaturedSource.from_db(db) | |
60 source.clear_featured(charm_data, 'charm') | |
61 | |
62 # Finally, remove all the revisions of the charm document from the | |
63 # database. | |
64 charm_query = CharmSource.make_all_revisions_query(charm_data) | |
65 db.charms.remove(charm_query) | |
66 return 0 | 52 return 0 |
67 | 53 |
68 | 54 |
69 def entry_point(): | 55 def entry_point(): |
70 """Script entry point for removing a server's start up timestamp.""" | 56 """Script entry point for removing a server's start up timestamp.""" |
71 sys.exit(main(sys.argv)) | 57 sys.exit(main(sys.argv)) |
OLD | NEW |