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.'})
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)
def _save_containers_parameters_list_in_file(containers_parameters_list, saved_containers_path): temp_file_path = saved_containers_path + '.tmp' with open(temp_file_path, 'w') as temp_file: json.dump(containers_parameters_list, temp_file, indent=4, sort_keys=True) shutil.copyfile(temp_file_path, saved_containers_path) os.remove(temp_file_path)
def main(): version = armada_api.get('version') if not is_valid_response(version): # skip sync if we cannot determine current version return r = requests.get('http://version.armada.sh/version_check', data=dict(version=version), timeout=3) r.raise_for_status() data = r.json() data.update({ 'displayed': 0, 'synced': time.time(), }) with SyncOpen(VERSION_CACHE_FILE_PATH, 'w') as f: json.dump(data, f)
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.'})
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)