<s> from itertools import chain <EOL> import re <EOL> from alembic import op <EOL> from oslo_serialization import jsonutils <EOL> import six <EOL> import sqlalchemy as sa <EOL> from sqlalchemy . sql import text <EOL> from nailgun import consts <EOL> from nailgun . settings import settings <EOL> def upgrade_enum ( table , column_name , enum_name , old_options , new_options ) : <EOL> old_type = sa . Enum ( * old_options , name = enum_name ) <EOL> new_type = sa . Enum ( * new_options , name = enum_name ) <EOL> tmp_type = sa . Enum ( * new_options , name = "<STR_LIT:_>" + enum_name ) <EOL> tmp_type . create ( op . get_bind ( ) , checkfirst = False ) <EOL> op . execute ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' . format ( <EOL> table , <EOL> column_name , <EOL> enum_name <EOL> ) <EOL> ) <EOL> old_type . drop ( op . get_bind ( ) , checkfirst = False ) <EOL> new_type . create ( op . get_bind ( ) , checkfirst = False ) <EOL> op . execute ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' . format ( <EOL> table , <EOL> column_name , <EOL> enum_name <EOL> ) <EOL> ) <EOL> tmp_type . drop ( op . get_bind ( ) , checkfirst = False ) <EOL> def drop_enum ( name ) : <EOL> op . execute ( <EOL> u'<STR_LIT>' . format ( name ) <EOL> ) <EOL> def convert_condition_value ( val ) : <EOL> if isinstance ( val , six . string_types ) : <EOL> return "<STR_LIT>" . format ( val ) <EOL> return str ( val ) . lower ( ) <EOL> def negate_condition ( condition ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . format ( condition ) <EOL> def remove_question_operator ( expression ) : <EOL> """<STR_LIT>""" <EOL> return re . sub ( r'<STR_LIT>' , '<STR_LIT>' , expression ) <EOL> def upgrade_release_attributes_50_to_51 ( attrs_meta ) : <EOL> if not attrs_meta . get ( '<STR_LIT>' ) : <EOL> return attrs_meta <EOL> def depends_to_restrictions ( depends , restrictions ) : <EOL> for cond in depends : <EOL> expr = cond . keys ( ) [ <NUM_LIT:0> ] <EOL> restrictions . append ( <EOL> expr + "<STR_LIT>" + convert_condition_value ( cond [ expr ] ) ) <EOL> def conflicts_to_restrictions ( conflicts , restrictions ) : <EOL> for cond in conflicts : <EOL> expr = cond . keys ( ) [ <NUM_LIT:0> ] <EOL> restrictions . append ( <EOL> expr + "<STR_LIT>" + convert_condition_value ( cond [ expr ] ) ) <EOL> for _ , group in six . iteritems ( attrs_meta . get ( '<STR_LIT>' ) ) : <EOL> for _ , attr in six . iteritems ( group ) : <EOL> restrictions = [ ] <EOL> if attr . get ( '<STR_LIT>' ) : <EOL> depends_to_restrictions ( attr [ '<STR_LIT>' ] , restrictions ) <EOL> attr . pop ( '<STR_LIT>' ) <EOL> if attr . get ( '<STR_LIT>' ) : <EOL> conflicts_to_restrictions ( attr [ '<STR_LIT>' ] , restrictions ) <EOL> attr . pop ( '<STR_LIT>' ) <EOL> if restrictions : <EOL> attr [ '<STR_LIT>' ] = restrictions <EOL> return attrs_meta <EOL> def upgrade_release_attributes_51_to_60 ( attrs_meta ) : <EOL> """<STR_LIT>""" <EOL> if not attrs_meta . get ( '<STR_LIT>' ) : <EOL> return attrs_meta <EOL> def convert_restrictions ( restrictions ) : <EOL> result = [ ] <EOL> for restriction in restrictions : <EOL> if isinstance ( restriction , six . string_types ) : <EOL> restriction = remove_question_operator ( restriction ) <EOL> else : <EOL> restriction [ '<STR_LIT>' ] = remove_question_operator ( <EOL> restriction [ '<STR_LIT>' ] ) <EOL> result . append ( restriction ) <EOL> return result <EOL> for _ , group in six . iteritems ( attrs_meta . get ( '<STR_LIT>' ) ) : <EOL> for _ , attr in six . iteritems ( group ) : <EOL> if '<STR_LIT>' in attr : <EOL> attr [ '<STR_LIT>' ] = convert_restrictions ( <EOL> attr [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in attr : <EOL> for value in attr [ '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in value : <EOL> value [ '<STR_LIT>' ] = convert_restrictions ( <EOL> value [ '<STR_LIT>' ] ) <EOL> return attrs_meta <EOL> def upgrade_release_roles_50_to_51 ( roles_meta ) : <EOL> for _ , role in six . iteritems ( roles_meta ) : <EOL> if role . get ( '<STR_LIT>' ) : <EOL> for depend in role [ '<STR_LIT>' ] : <EOL> cond = depend . get ( '<STR_LIT>' ) <EOL> if isinstance ( cond , dict ) : <EOL> expr = cond . keys ( ) [ <NUM_LIT:0> ] <EOL> depend [ '<STR_LIT>' ] = expr + "<STR_LIT>" + convert_condition_value ( cond [ expr ] ) <EOL> return roles_meta <EOL> def upgrade_release_roles_51_to_60 ( roles_meta , add_meta = None ) : <EOL> """<STR_LIT>""" <EOL> add_meta = add_meta or { } <EOL> for role_name , role in six . iteritems ( roles_meta ) : <EOL> for depend in role . get ( '<STR_LIT>' , [ ] ) : <EOL> cond = depend . get ( '<STR_LIT>' ) <EOL> new_restriction = { <EOL> '<STR_LIT>' : remove_question_operator ( negate_condition ( cond ) ) <EOL> } <EOL> if '<STR_LIT>' in depend : <EOL> new_restriction [ '<STR_LIT:message>' ] = depend [ '<STR_LIT>' ] <EOL> role . setdefault ( '<STR_LIT>' , [ ] ) <EOL> role [ '<STR_LIT>' ] . append ( new_restriction ) <EOL> if '<STR_LIT>' in role : <EOL> del role [ '<STR_LIT>' ] <EOL> if role_name in add_meta : <EOL> role . update ( add_meta [ role_name ] ) <EOL> return roles_meta <EOL> def upgrade_clusters_replaced_info ( connection ) : <EOL> select = text ( <EOL> """<STR_LIT>""" ) <EOL> clusters = connection . execute ( select ) <EOL> for cluster in clusters : <EOL> nodes_select = text ( <EOL> """<STR_LIT>""" ) <EOL> nodes = connection . execute ( <EOL> nodes_select , <EOL> id = cluster [ <NUM_LIT:0> ] ) <EOL> provisioning_info = jsonutils . loads ( cluster [ <NUM_LIT:1> ] ) <EOL> deployment_nodes = jsonutils . loads ( cluster [ <NUM_LIT:2> ] ) <EOL> provisioning_nodes = provisioning_info . pop ( '<STR_LIT>' , [ ] ) <EOL> for node in nodes : <EOL> node_deploy = [ d for d in deployment_nodes <EOL> if d [ '<STR_LIT>' ] == str ( node [ <NUM_LIT:0> ] ) ] <EOL> node_provision = next ( ( d for d in provisioning_nodes <EOL> if d [ '<STR_LIT>' ] == str ( node [ <NUM_LIT:0> ] ) ) , { } ) <EOL> update_node = text ( <EOL> """<STR_LIT>""" ) <EOL> connection . execute ( <EOL> update_node , <EOL> deploy = jsonutils . dumps ( node_deploy ) , <EOL> provision = jsonutils . dumps ( node_provision ) , <EOL> id = node [ <NUM_LIT:0> ] ) <EOL> update_cluster = text ( <EOL> """<STR_LIT>""" ) <EOL> connection . execute ( <EOL> update_cluster , <EOL> deploy = jsonutils . dumps ( [ ] ) , <EOL> provision = jsonutils . dumps ( provisioning_info ) , <EOL> id = cluster [ <NUM_LIT:0> ] ) <EOL> def upgrade_release_set_deployable_false ( connection , versions ) : <EOL> """<STR_LIT>""" <EOL> update_query = text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> connection . execute ( update_query , versions = tuple ( versions ) ) <EOL> def upgrade_release_fill_orchestrator_data ( connection , versions ) : <EOL> """<STR_LIT>""" <EOL> for version in versions : <EOL> select_query = text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> releases = connection . execute ( select_query , version = version ) <EOL> for release in releases : <EOL> insert_query = text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> repo_path = '<STR_LIT>' . format ( <EOL> MASTER_IP = settings . MASTER_IP , OS = release [ <NUM_LIT:1> ] . lower ( ) ) <EOL> if release [ <NUM_LIT:1> ] . lower ( ) == '<STR_LIT>' : <EOL> repo_path += '<STR_LIT>' <EOL> connection . execute ( <EOL> insert_query , <EOL> release_id = release [ <NUM_LIT:0> ] , <EOL> repo_metadata = ( <EOL> '<STR_LIT>' . format ( repo_path ) ) , <EOL> puppet_manifests_source = ( <EOL> '<STR_LIT>' . format ( <EOL> MASTER_IP = settings . MASTER_IP ) ) , <EOL> puppet_modules_source = ( <EOL> '<STR_LIT>' . format ( <EOL> MASTER_IP = settings . MASTER_IP ) ) , <EOL> ) <EOL> def move_orchestrator_data_to_attributes ( connection ) : <EOL> """<STR_LIT>""" <EOL> select_query = text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for odata in connection . execute ( select_query ) : <EOL> select_query = text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for release in connection . execute ( select_query , release_id = odata [ <NUM_LIT:1> ] ) : <EOL> repo_setup = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> } ] , <EOL> '<STR_LIT:label>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:50> , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : [ ] , <EOL> } } <EOL> puppet = { <EOL> '<STR_LIT>' : odata [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : odata [ <NUM_LIT:4> ] , <EOL> } <EOL> if release [ <NUM_LIT:2> ] . lower ( ) == '<STR_LIT>' : <EOL> for name , repo in six . iteritems ( jsonutils . loads ( odata [ <NUM_LIT:2> ] ) ) : <EOL> uri , suite , section = repo . split ( ) <EOL> repo_setup [ '<STR_LIT>' ] [ '<STR_LIT:value>' ] . append ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : uri , <EOL> '<STR_LIT>' : suite , <EOL> '<STR_LIT>' : section , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } ) <EOL> elif release [ <NUM_LIT:2> ] . lower ( ) == '<STR_LIT>' : <EOL> for name , repo in six . iteritems ( jsonutils . loads ( odata [ <NUM_LIT:2> ] ) ) : <EOL> repo_setup [ '<STR_LIT>' ] [ '<STR_LIT:value>' ] . append ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : repo , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } ) <EOL> attributes_metadata = jsonutils . loads ( release [ <NUM_LIT:1> ] ) <EOL> attributes_metadata [ '<STR_LIT>' ] . update ( { '<STR_LIT>' : repo_setup } ) <EOL> attributes_metadata [ '<STR_LIT>' ] . update ( { '<STR_LIT>' : puppet } ) <EOL> update_query = text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> connection . execute ( <EOL> update_query , <EOL> attributes_metadata = jsonutils . dumps ( attributes_metadata ) , <EOL> release_id = odata [ <NUM_LIT:1> ] ) <EOL> select_query = text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for attr in connection . execute ( select_query , release_id = odata [ <NUM_LIT:1> ] ) : <EOL> editable = jsonutils . loads ( attr [ <NUM_LIT:1> ] ) <EOL> generated = jsonutils . loads ( attr [ <NUM_LIT:2> ] ) <EOL> editable . update ( { '<STR_LIT>' : repo_setup } ) <EOL> generated . update ( { '<STR_LIT>' : puppet } ) <EOL> connection . execute ( <EOL> text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> editable = jsonutils . dumps ( editable ) , <EOL> generated = jsonutils . dumps ( generated ) , <EOL> attr_id = attr [ <NUM_LIT:0> ] ) <EOL> def upgrade_attributes_metadata_6_0_to_6_1 ( attributes_meta ) : <EOL> attributes_meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:description>' ] = '<STR_LIT>' <EOL> attributes_meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> "<STR_LIT:value>" : False , <EOL> "<STR_LIT>" : <NUM_LIT:30> , <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } <EOL> return attributes_meta <EOL> def upgrade_master_node_settings_6_0_to_6_1 ( master_node_settings ) : <EOL> return master_node_settings <EOL> def upgrade_role_limits_6_0_to_6_1 ( roles_meta , _limits_to_update ) : <EOL> for role_name , role_definition in six . iteritems ( roles_meta ) : <EOL> if role_name in _limits_to_update : <EOL> role_definition [ '<STR_LIT>' ] = _limits_to_update [ role_name ] <EOL> return roles_meta <EOL> def upgrade_role_restrictions_6_0_to_6_1 ( roles_meta , _new_role_restrictions ) : <EOL> for role_name , role_definition in six . iteritems ( roles_meta ) : <EOL> if role_name in _new_role_restrictions : <EOL> role_definition [ '<STR_LIT>' ] = _new_role_restrictions [ role_name ] <EOL> return roles_meta <EOL> def upgrade_vip_types_6_0_to_6_1 ( connection ) : <EOL> update_query_node_null = text ( <EOL> "<STR_LIT>" ) <EOL> connection . execute ( update_query_node_null , <EOL> haproxy = consts . NETWORK_VIP_NAMES_V6_1 . haproxy ) <EOL> def downgrade_vip_types_6_1_to_6_0 ( connection ) : <EOL> delete_query = text ( <EOL> "<STR_LIT>" ) <EOL> connection . execute ( <EOL> delete_query , <EOL> haproxy = consts . NETWORK_VIP_NAMES_V6_1 . haproxy <EOL> ) <EOL> def upgrade_6_0_to_6_1_plugins_cluster_attrs_use_ids_mapping ( connection ) : <EOL> """<STR_LIT>""" <EOL> select_attrs = text ( """<STR_LIT>""" ) <EOL> select_plugins = text ( <EOL> """<STR_LIT>""" ) <EOL> update_attrs = text ( <EOL> """<STR_LIT>""" ) <EOL> attrs_list = connection . execute ( select_attrs ) <EOL> for raw_attrs in attrs_list : <EOL> attr_id = raw_attrs [ <NUM_LIT:0> ] <EOL> attrs = jsonutils . loads ( raw_attrs [ <NUM_LIT:1> ] ) <EOL> for key , attr in six . iteritems ( attrs ) : <EOL> metadata = attr . get ( '<STR_LIT>' , { } ) <EOL> plugin_version = metadata . get ( '<STR_LIT>' ) <EOL> if not plugin_version : <EOL> continue <EOL> plugin_name = key <EOL> plugin_id = None <EOL> plugins = list ( connection . execute ( <EOL> select_plugins , <EOL> plugin_name = plugin_name , <EOL> plugin_version = plugin_version ) ) <EOL> if plugins : <EOL> plugin_id = plugins [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> del attr [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> attr [ '<STR_LIT>' ] [ '<STR_LIT>' ] = plugin_id <EOL> connection . execute ( <EOL> update_attrs , <EOL> editable = jsonutils . dumps ( attrs ) , <EOL> id = attr_id ) <EOL> def upgrade_networks_metadata_to_6_1 ( networks_meta , _bonding_metadata ) : <EOL> networks_meta [ '<STR_LIT>' ] = _bonding_metadata <EOL> nets = [ k for k , v in six . iteritems ( networks_meta ) if v . get ( '<STR_LIT>' ) ] <EOL> for network in chain ( * [ networks_meta [ net ] [ '<STR_LIT>' ] for net in nets ] ) : <EOL> network = create_default_vips ( network ) <EOL> return networks_meta <EOL> def upgrade_network_groups_metadata_6_0_to_6_1 ( connection ) : <EOL> select_query = text ( "<STR_LIT>" ) <EOL> update_query = text ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> net_groups = connection . execute ( select_query ) <EOL> for ng_id , ng_meta in net_groups : <EOL> updated_meta = create_default_vips ( jsonutils . loads ( ng_meta ) ) <EOL> connection . execute ( <EOL> update_query , <EOL> id = ng_id , <EOL> meta = jsonutils . dumps ( updated_meta ) <EOL> ) <EOL> def create_default_vips ( network ) : <EOL> if "<STR_LIT>" in network : <EOL> if network [ "<STR_LIT>" ] : <EOL> network [ "<STR_LIT>" ] = [ consts . NETWORK_VIP_NAMES_V6_1 . haproxy ] <EOL> del network [ "<STR_LIT>" ] <EOL> return network <EOL> def upgrade_ubuntu_cobbler_profile_6_0_to_6_1 ( connection ) : <EOL> select_query = text ( "<STR_LIT>" ) <EOL> update_query = text ( <EOL> "<STR_LIT>" ) <EOL> for attr_id , generated in connection . execute ( select_query ) : <EOL> attrs = jsonutils . loads ( generated ) <EOL> if attrs [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> attrs [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> connection . execute ( <EOL> update_query , <EOL> generated = jsonutils . dumps ( attrs ) , <EOL> attr_id = attr_id ) <EOL> select_query = text ( "<STR_LIT>" ) <EOL> update_query = text ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for release_id , attributes_metadata in connection . execute ( select_query ) : <EOL> attrs = jsonutils . loads ( attributes_metadata ) <EOL> if attrs [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> attrs [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> connection . execute ( <EOL> update_query , <EOL> attrs_meta = jsonutils . dumps ( attrs ) , <EOL> release_id = release_id ) <EOL> def upgrade_cluster_attributes_6_0_to_6_1 ( connection ) : <EOL> select_query = text ( """<STR_LIT>""" ) <EOL> update_query = text ( <EOL> """<STR_LIT>""" ) <EOL> for attr_id , editable in connection . execute ( select_query ) : <EOL> attributes = jsonutils . loads ( editable ) <EOL> attributes [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> "<STR_LIT:value>" : False , <EOL> "<STR_LIT>" : <NUM_LIT:30> , <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } <EOL> connection . execute ( <EOL> update_query , <EOL> editable = jsonutils . dumps ( attributes ) , <EOL> attr_id = attr_id ) </s>
