def pod(**kwargs): if 'owner_id' not in kwargs and 'owner' not in kwargs: kwargs['owner'], _ = user_fixtures() if 'kube_id' not in kwargs and 'kube' not in kwargs: kwargs['kube'] = Kube.get_default_kube() if 'config' in kwargs and not isinstance(kwargs['config'], basestring): kwargs['config'] = json.dumps(kwargs['config']) namespace = str(uuid4()) kwargs.setdefault('id', namespace) kwargs.setdefault('name', 'pod-' + randstr()) kwargs.setdefault( 'config', json.dumps({ 'node': None, 'replicas': 1, 'secrets': [], 'namespace': namespace, 'restartPolicy': 'Never', 'volumes': [], 'sid': str(uuid4()), 'containers': [{ 'kubes': 1, 'terminationMessagePath': None, 'name': 'curl' + randstr(), 'workingDir': '', 'image': 'appropriate/curl', 'args': ['curl', 'httpbin.org/get'], 'volumeMounts': [], 'sourceUrl': 'hub.docker.com/r/appropriate/curl', 'env': [{ 'name': 'PATH', 'value': '/usr/local/sbin:/usr/local/bin:' '/usr/sbin:/usr/bin:/sbin:/bin' }], 'ports': [] }] })) return Pod(**kwargs).save()
def user_fixtures(admin=False, active=True, **kwargs): username = '******' + randstr(8) password = randstr(10) role_id = Role.filter_by( rolename='User' if not admin else 'Admin').first().id email = randstr(10) + '@test.test' data = dict(username=username, password=password, active=active, role_id=role_id, package_id=0, email=email) user = User(**dict(data, **kwargs)).save() return user, password
def predefined_app(**kwargs): db_app = PredefinedApp(name=kwargs.get('name', randstr())) db.session.add(db_app) for key, value in kwargs.items(): setattr(db_app, key, value) db.session.commit() return db_app
def node(hostname=None, ip=None, kube_id=None, owner=None): if owner is None: owner, _ = user_fixtures() if kube_id is None: kube_id = Kube.get_default_kube() if ip is None: ip = random_ip() if hostname is None: hostname = randstr() return Node(ip=ip, hostname=hostname, kube_id=kube_id.id, state=NODE_STATUSES.pending)
def check_name(cls, name, owner_id, pod_id=None, generate_new=False, max_retries=5): query = cls.query.filter( cls.name == name, cls.owner_id == owner_id ) if pod_id is not None: query = query.filter(cls.id != pod_id) duplicate = query.first() if duplicate: if generate_new: new_name = '{}-{}'.format(name, utils.randstr(3, string.digits)) retries = max_retries while retries > 0: try: return cls.check_name(new_name, owner_id, pod_id) except PodExists: new_name = '{}-{}'.format( name, utils.randstr(3, string.digits)) retries -= 1 raise PodExists(name=name, id_=duplicate.id) return name
def initial_fixtures(): """Almost the same stuff as manage.py createdb""" add_kubes_and_packages() add_system_settings() add_notifications() add_all_permissions() add_users_and_roles(randstr()) generate_menu() # Fix packages id next val db.engine.execute("SELECT setval('packages_id_seq', 1, false)")
def upgrade(cls, upd, with_testing): fdata = open(KUBERDOCK_SETTINGS_FILE).read() if 'SECRET_KEY=' not in fdata: upd.print_log('Generating secret key...') with open(KUBERDOCK_SETTINGS_FILE, 'a') as c: c.write("SECRET_KEY={0}\n".format(randstr(32, secure=True)))
def kube_type(**kwargs): return Kube(**dict( kwargs, name=randstr(), cpu=.25, memory=64, disk_space=1)).save()
def upgrade(upd, with_testing, *args, **kwargs): upgrade_db() # === 00124_update.py === # Move index file of k8s2etcd service from / to /var/lib/kuberdock try: stop_service(u124_service_name) if os.path.isfile(u124_old) and not os.path.isfile(u124_new): shutil.move(u124_old, u124_new) finally: start_service(u124_service_name) # === 00126_update.py === pod_collection = PodCollection() for pod_dict in pod_collection.get(as_json=False): pod = pod_collection._get_by_id(pod_dict['id']) db_config = get_pod_config(pod.id) cluster_ip = db_config.pop('clusterIP', None) if cluster_ip is None: service_name = db_config.get('service') if service_name is None: continue namespace = db_config.get('namespace') or pod.id service = KubeQuery().get(['services', service_name], ns=namespace) cluster_ip = service.get('spec', {}).get('clusterIP') if cluster_ip is not None: db_config['podIP'] = cluster_ip replace_pod_config(pod, db_config) # === 00127_update.py === upd.print_log('Upgrading menu...') MenuItemRole.query.delete() MenuItem.query.delete() Menu.query.delete() generate_menu() # === 00130_update.py === upd.print_log('Update permissions...') Permission.query.delete() Resource.query.delete() add_permissions() db.session.commit() # === 00135_update.py === # upd.print_log('Changing session_data schema...') # upgrade_db(revision='220dacf65cba') # === 00137_update.py === upd.print_log('Upgrading db...') # upgrade_db(revision='3c832810a33c') upd.print_log('Raise max kubes to 64') max_kubes = 'max_kubes_per_container' old_value = SystemSettings.get_by_name(max_kubes) if old_value == '10': SystemSettings.set_by_name(max_kubes, 64) upd.print_log('Update kubes') small = Kube.get_by_name('Small') standard = Kube.get_by_name('Standard') if small: small.cpu = 0.12 small.name = 'Tiny' small.memory = 64 if small.is_default and standard: small.is_default = False standard.is_default = True small.save() if standard: standard.cpu = 0.25 standard.memory = 128 standard.save() high = Kube.get_by_name('High memory') if high: high.cpu = 0.25 high.memory = 256 high.disk_space = 3 high.save() # === 00138_update.py === if not (CEPH or AWS): upgrade_localstorage_paths(upd) # === added later === secret_key = SystemSettings.query.filter( SystemSettings.name == 'sso_secret_key').first() if not secret_key.value: secret_key.value = randstr(16) secret_key.description = ( 'Used for Single sign-on. Must be shared between ' 'Kuberdock and billing system or other 3rd party ' 'application.') db.session.commit() upd.print_log('Close all sessions...') close_all_sessions()