예제 #1
0
def _get_runtime_settings():
    try:
        shutil.copy(consul_config.RUNTIME_SETTINGS_PATH, consul_config.ORIGINAL_RUNTIME_SETTINGS_PATH)
        with open(consul_config.ORIGINAL_RUNTIME_SETTINGS_PATH) as runtime_settings_json:
            runtime_settings = json.load(runtime_settings_json)
    except:
        runtime_settings = {}

    try:
        with open(consul_config.OVERRIDE_RUNTIME_SETTINGS_PATH) as runtime_settings_json:
            runtime_settings.update(json.load(runtime_settings_json))
    except:
        pass

    ship_ips = runtime_settings.get('ships', [])
    consul_mode = consul_config.ConsulMode.BOOTSTRAP
    if runtime_settings.get('is_commander') is True:
        if ship_ips and len(ship_ips) > 0:
            consul_mode = consul_config.ConsulMode.SERVER
    if runtime_settings.get('is_commander') is False:
        consul_mode = consul_config.ConsulMode.CLIENT

    if runtime_settings.get('datacenter'):
        datacenter = runtime_settings.get('datacenter')
    else:
        datacenter = 'dc-' + str(random.randrange(1000000))

    ship_name = runtime_settings.get('name')

    return consul_mode, ship_ips, datacenter, ship_name
예제 #2
0
def _version_check():
    current_version = armada_api.get('version')
    if not is_valid_response(current_version):
        # skip version check since we cannot determinate current version
        return

    with SyncOpen(VERSION_CACHE_FILE_PATH, 'r+') as f:
        data = json.load(f)
        displayed_timestamp = data['displayed']

        cache_version = data['latest_version']
        current_is_newer = StrictVersion(current_version) >= StrictVersion(
            cache_version)
        if current_is_newer or time.time(
        ) - DISPLAY_INTERVAL < displayed_timestamp:
            return

        message = 'You are using armada version {}, however version {} is available. ' \
                  'You should consider upgrading armada via "bash <(curl -sL http://armada.sh/install)"' \
            .format(armada_api.get('version'), data['latest_version'])
        print('\n' + message, file=sys.stderr)

        data['displayed'] = time.time()
        f.seek(0)
        f.truncate()
        json.dump(data, f)
예제 #3
0
    def POST(self):
        try:
            # 'startsecs=0' is to avoid restarting consul after `consul leave`.
            os.system(
                'sed -i \'/autorestart=true/cautorestart=false\' /etc/supervisor/conf.d/consul.conf'
            )
            os.system('echo startsecs=0 >> /etc/supervisor/conf.d/consul.conf')

            os.system('supervisorctl update consul')

            # As 'supervisorctl update' will restart Consul, we have to wait for it to be running.
            deadline = time.time() + 15
            while time.time() < deadline:
                try:
                    get_current_datacenter()
                    break
                except:
                    time.sleep(1)

            deregister_services(gethostname())
            os.system('consul leave')
        finally:
            post_data = json.loads(web.data() or '{}')
            runtime_settings_path = '/opt/armada/runtime_settings.json'
            if not post_data.get('keep-joined') and os.path.isfile(
                    runtime_settings_path):
                with open(runtime_settings_path) as f:
                    runtime_settings = json.load(f)
                runtime_settings['ships'] = []
                runtime_settings['is_commander'] = True
                with open(runtime_settings_path, 'w') as f:
                    json.dump(runtime_settings, f, sort_keys=True, indent=4)
        return self.status_ok({'message': 'Shutdown complete.'})
예제 #4
0
파일: update.py 프로젝트: labuzm/armada
def _cache_outdated_or_invalid():
    try:
        with SyncOpen(VERSION_CACHE_FILE_PATH, 'r') as f:
            data = json.load(f)
    except (IOError, ValueError):
        return True
    synced_timestamp = data['synced']
    return time.time() - SYNC_INTERVAL > synced_timestamp
예제 #5
0
def _cache_outdated_or_invalid():
    try:
        with SyncOpen(VERSION_CACHE_FILE_PATH, 'r') as f:
            data = json.load(f)
    except (IOError, ValueError):
        return True
    synced_timestamp = data['synced']
    return time.time() - SYNC_INTERVAL > synced_timestamp
예제 #6
0
def _get_current_consul_mode():
    with open(consul_config.CONFIG_PATH) as consul_config_json:
        consul_config_data = json.load(consul_config_json)

    if consul_config_data.get('bootstrap_expect'):
        if _get_armada_size() > 1:
            return consul_config.ConsulMode.SERVER
        else:
            return consul_config.ConsulMode.BOOTSTRAP
    if consul_config_data.get('server'):
        return consul_config.ConsulMode.SERVER
    return consul_config.ConsulMode.CLIENT
예제 #7
0
def command_recover(args):
    if not args.saved_containers_path:
        payload = {'recover_from_kv': True}
    else:
        with open(args.saved_containers_path) as saved_containers_file:
            saved_containers = json.load(saved_containers_file)
        payload = {'recover_from_kv': False, 'saved_containers': saved_containers}
    result = armada_api.post('recover', payload)
    if result['status'] != 'ok':
        print(result['error'])
        sys.exit(1)
    else:
        print('Containers have been restored.')
예제 #8
0
def command_recover(args):
    if not args.saved_containers_path:
        payload = {'recover_from_kv': True}
    else:
        with open(args.saved_containers_path) as saved_containers_file:
            saved_containers = json.load(saved_containers_file)
        payload = {
            'recover_from_kv': False,
            'saved_containers': saved_containers
        }
    result = armada_api.post('recover', payload)
    if result['status'] != 'ok':
        print(result['error'])
        sys.exit(1)
    else:
        print('Containers have been restored.')
예제 #9
0
def _init_dockyards():
    try:
        with open(consul_config.ORIGINAL_RUNTIME_SETTINGS_PATH) as runtime_settings_json:
            runtime_settings = json.load(runtime_settings_json)
    except:
        runtime_settings = {}

    # Initialize dockyard list with fallback dockyard.
    if not alias.get_alias(alias.DOCKYARD_FALLBACK_ALIAS):
        alias.set_alias(alias.DOCKYARD_FALLBACK_ALIAS, alias.DOCKYARD_FALLBACK_ADDRESS)

    dockyards = runtime_settings.get('dockyards', {})
    default_alias = None
    for info in dockyards:
        dockyard_alias = info.get('name')
        if dockyard_alias and not alias.get_alias(dockyard_alias):
            alias.set_alias(dockyard_alias, info.get('address'), info.get('user'), info.get('password'))
            if info.get('is_default'):
                default_alias = dockyard_alias
    if default_alias:
        alias.set_default(default_alias)
예제 #10
0
def _init_dockyards():
    try:
        with open(consul_config.ORIGINAL_RUNTIME_SETTINGS_PATH) as runtime_settings_json:
            runtime_settings = json.load(runtime_settings_json)
    except:
        runtime_settings = {}

    # Initialize dockyard list with fallback dockyard.
    if not alias.get_alias(alias.DOCKYARD_FALLBACK_ALIAS):
        alias.set_alias(alias.DOCKYARD_FALLBACK_ALIAS, alias.DOCKYARD_FALLBACK_ADDRESS)

    dockyards = runtime_settings.get('dockyards', {})
    default_alias = None
    for info in dockyards:
        dockyard_alias = info.get('name')
        if dockyard_alias and not alias.get_alias(dockyard_alias):
            alias.set_alias(dockyard_alias, info.get('address'), info.get('user'), info.get('password'))
            if info.get('is_default'):
                default_alias = dockyard_alias
    if default_alias:
        alias.set_default(default_alias)
예제 #11
0
    def on_post(self, req, resp):
        try:
            # 'startsecs=0' is to avoid restarting consul after `consul leave`.
            check_call(
                'sed -i \'/autorestart=true/cautorestart=false\' /etc/supervisor/conf.d/consul.conf',
                shell=True)
            check_call(
                'echo startsecs=0 >> /etc/supervisor/conf.d/consul.conf',
                shell=True)

            check_call(['supervisorctl', 'update', 'consul'])

            # As 'supervisorctl update' will restart Consul, we have to wait for it to be running.
            deadline = time.time() + 15
            ok = False
            while time.time() < deadline:
                try:
                    get_current_datacenter()
                    ok = True
                    break
                except Exception:
                    time.sleep(1)
            if not ok:
                get_logger().warn('Restarting consul timed out.')

            deregister_services(gethostname())
            check_call(['consul', 'leave'])
        finally:
            post_data = req.json
            runtime_settings_path = '/opt/armada/runtime_settings.json'
            if not post_data.get('keep-joined') and os.path.isfile(
                    runtime_settings_path):
                with open(runtime_settings_path) as f:
                    runtime_settings = json.load(f)
                runtime_settings['ships'] = []
                runtime_settings['is_commander'] = True
                with open(runtime_settings_path, 'w') as f:
                    json.dump(runtime_settings, f, sort_keys=True, indent=4)
        return self.status_ok(resp, {'message': 'Shutdown complete.'})
예제 #12
0
파일: update.py 프로젝트: labuzm/armada
def _version_check():
    current_version = armada_api.get('version')
    if not is_valid_response(current_version):
        # skip version check since we cannot determinate current version
        return

    with SyncOpen(VERSION_CACHE_FILE_PATH, 'r+') as f:
        data = json.load(f)
        displayed_timestamp = data['displayed']

        cache_version = data['latest_version']
        current_is_newer = StrictVersion(current_version) >= StrictVersion(cache_version)
        if current_is_newer or time.time() - DISPLAY_INTERVAL < displayed_timestamp:
            return

        message = 'You are using armada version {}, however version {} is available. ' \
                  'You should consider upgrading armada via "bash <(curl -sL http://armada.sh/install)"' \
            .format(armada_api.get('version'), data['latest_version'])
        print('\n' + message, file=sys.stderr)

        data['displayed'] = time.time()
        f.seek(0)
        f.truncate()
        json.dump(data, f)
예제 #13
0
def _load_saved_containers_parameters_list(running_containers_parameters_path):
    with open(running_containers_parameters_path) as f:
        return json.load(f)
def _load_saved_containers_parameters(running_containers_parameters_path):
    with open(running_containers_parameters_path) as f:
        return json.load(f)