Completed
Pull Request — master (#2669)
by Lakshmi
11:08 queued 04:46
created

migrate_datastore()   A

Complexity

Conditions 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 16
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.system import SYSTEM_KV_PREFIX
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
            secret = getattr(kvp, 'secret', False)
37
            encrypted = getattr(kvp, 'encrypted', False)
38
            scope = getattr(kvp, 'scope', SYSTEM_KV_PREFIX)
39
            new_kvp_db = KeyValuePairDB(id=kvp.id, name=kvp.name,
40
                                        expire_timestamp=kvp.expire_timestamp,
41
                                        value=kvp.value, secret=secret, encrypted=encrypted,
42
                                        scope=scope)
43
            KeyValuePair.add_or_update(new_kvp_db)
44
    except:
45
        print('Error trying to migrate KeyValuePair item with name: %s' % kvp.name)
46
        tb.print_exc()
47
48
49
def main():
50
    config.parse_args()
51
52
    # Connect to db.
53
    db_setup()
54
55
    # Migrate rules.
56
    migrate_datastore()
57
58
    # Disconnect from db.
59
    db_teardown()
60
61
62
if __name__ == '__main__':
63
    main()
64