Passed
Push — develop ( 71cfc9...483b2a )
by Plexxi
07:06 queued 03:42
created

migrate_datastore()   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 17
rs 9.4285
1
#!/usr/bin/env python
2
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
3
# contributor license agreements.  See the NOTICE file distributed with
4
# this work for additional information regarding copyright ownership.
5
# The ASF licenses this file to You under the Apache License, Version 2.0
6
# (the "License"); you may not use this file except in compliance with
7
# the License.  You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
17
import traceback as tb
18
19
from st2common import config
20
from st2common.constants.keyvalue import SYSTEM_SCOPE
21
from st2common.models.db.keyvalue import KeyValuePairDB
22
from st2common.persistence.keyvalue import KeyValuePair
23
from st2common.service_setup import db_setup
24
from st2common.service_setup import db_teardown
25
26
27
class DatastoreMigration(object):
28
    pass
29
30
31
def migrate_datastore():
32
    key_value_items = KeyValuePair.get_all()
33
34
    try:
35
        for kvp in key_value_items:
36
            kvp_id = getattr(kvp, 'id', None)
37
            secret = getattr(kvp, 'secret', False)
38
            encrypted = getattr(kvp, 'encrypted', False)
39
            scope = getattr(kvp, 'scope', SYSTEM_SCOPE)
40
            new_kvp_db = KeyValuePairDB(id=kvp_id, name=kvp.name,
41
                                        expire_timestamp=kvp.expire_timestamp,
42
                                        value=kvp.value, secret=secret, encrypted=encrypted,
43
                                        scope=scope)
44
            KeyValuePair.add_or_update(new_kvp_db)
45
    except:
46
        print('Error trying to migrate KeyValuePair item with name: %s' % kvp.name)
47
        tb.print_exc()
48
49
50
def main():
51
    config.parse_args()
52
53
    # Connect to db.
54
    db_setup()
55
56
    # Migrate rules.
57
    migrate_datastore()
58
59
    # Disconnect from db.
60
    db_teardown()
61
62
63
if __name__ == '__main__':
64
    main()
65