Esempio n. 1
0
def menu_show_clusters():
    json_data = cm.cluster_ls()
    if json_data == None: return
    
    cluster_name_list = json_data['data']['list']

    if len(cluster_name_list) == 0:
        print yellow('There is no cluster.')
        return

    cluster_name_list.sort()

    while True:
        # Get max length of cluster names
        max_len = 0
        for i in range(len(cluster_name_list)):
            if max_len < len(cluster_name_list[i]):
                max_len = len(cluster_name_list[i])
        fmt = '%%3d. %%-%ds   ' % max_len

        # Show cluster list
        COLUMN_CNT = 3
        line_no = len(cluster_name_list) / COLUMN_CNT
        remainder = len(cluster_name_list) % COLUMN_CNT 
        if remainder != 0:
            line_no += 1

        lines = []
        for i in range(line_no):
            lines.append('')

        print yellow('  0. all')
        item = 0
        for i in range(min(COLUMN_CNT, len(cluster_name_list))):
            line_to = line_no
            if remainder != 0 and remainder < (i + 1):
                line_to -= 1

            for j in range(line_to):
                lines[j] += yellow(fmt % (item+1, cluster_name_list[item]))
                item += 1

        for line in lines:
            print line

        print yellow('  x. Up')

        # Select a cluster
        print cyan("Select a cluster")
        try:
            s = prompt(">>")
            if s.lower() == 'x':
                break
            menu_no = int(s)

        except:
            try:
                warn(red('Invalid input', menu_no))
            except:
                pass
            continue

        if menu_no < 0 or menu_no > len(cluster_name_list):
            warn(red('Invalid menu'))
            continue

        if menu_no == 0:
            memlog = prompt_show_memlog()
            for i in range(len(cluster_name_list)):
                cluster_name = cluster_name_list[i].encode('ascii')
                print cyan(' #### ' + cluster_name + ' #### ')
                show_all(cluster_name, memlog)
            continue
    
        menu_select_info(cluster_name_list[menu_no-1].encode('ascii'))
Esempio n. 2
0
def menu_show_clusters():
    json_data = cm.cluster_ls()
    if json_data == None: return
    
    cluster_name_list = json_data['data']['list']

    if len(cluster_name_list) == 0:
        print yellow('There is no cluster.')
        return

    cluster_name_list.sort()

    while True:
        # Get max length of cluster names
        max_len = 0
        for i in range(len(cluster_name_list)):
            if max_len < len(cluster_name_list[i]):
                max_len = len(cluster_name_list[i])
        fmt = '%%3d. %%-%ds   ' % max_len

        # Show cluster list
        COLUMN_CNT = 3
        line_no = len(cluster_name_list) / COLUMN_CNT
        remainder = len(cluster_name_list) % COLUMN_CNT 
        if remainder != 0:
            line_no += 1

        lines = []
        for i in range(line_no):
            lines.append('')

        print yellow('  0. all')
        item = 0
        for i in range(min(COLUMN_CNT, len(cluster_name_list))):
            line_to = line_no
            if remainder != 0 and remainder < (i + 1):
                line_to -= 1

            for j in range(line_to):
                lines[j] += yellow(fmt % (item+1, cluster_name_list[item]))
                item += 1

        for line in lines:
            print line

        print yellow('  x. Up')

        # Select a cluster
        print cyan("Select a cluster")
        try:
            s = prompt(">>")
            if s.lower() == 'x':
                break
            menu_no = int(s)

        except:
            try:
                warn(red('Invalid input', menu_no))
            except:
                pass
            continue

        if menu_no < 0 or menu_no > len(cluster_name_list):
            warn(red('Invalid menu'))
            continue

        if menu_no == 0:
            memlog = prompt_show_memlog()
            for i in range(len(cluster_name_list)):
                cluster_name = cluster_name_list[i].encode('ascii')
                print cyan(' #### ' + cluster_name + ' #### ')
                show_all(cluster_name, memlog)
            continue
    
        menu_select_info(cluster_name_list[menu_no-1].encode('ascii'))
Esempio n. 3
0
def update_jobs():
    global g_update_jobs_cnt 
    global g_sched
    global g_old_appdata

    log_info('running...')

    out_cluster_ls = cm.cluster_ls()
    if out_cluster_ls.cm == False:
        log_info("CONFIG > mgmt-cc error.")
        return 

    # Get jobs from CM
    appdata_list = []
    for cluster_name in out_cluster_ls.json['data']['list']:
        # Appdata contains job of backup
        out = cm.appdata_get(cluster_name.encode('ascii'))
        if out.cm == False:
            log_info("CONFIG > mgmt-cc error.")
            return

        if out.appdata_list == None:
            log_info("CONFIG > Cluster '%s' doesn't have any appdata" % (cluster_name))
        else:
            for data in out.appdata_list:
                appdata_list.append(data)

    # Update jobs
    changed = False
    for appdata in appdata_list:
        period = appdata['period'].split(' ')
        y = period[5]
        day_of_week = period[4]
        mo = period[3]
        d = period[2]
        h = period[1]
        mi = period[0]

        old_appdata = find_in_list_of_dicts(g_old_appdata, "name", appdata["name"])

        if len(old_appdata) == 0:
            old_appdata = None
        else:
            old_appdata = old_appdata[0]

        try:
            # Add new job
            if old_appdata == None:
                sort_keys=True,
                log_info('CONFIG > add job : %s' % json_to_str(appdata))
                if h == 24:
                    h = 0

                if verifyAppdata(appdata):
                    g_sched.add_cron_job(backup, name=appdata['name'], args=[appdata], 
                            minute=mi, hour=h, day=d, month=mo, day_of_week=day_of_week, year=y)
                    changed = True
                else:
                    log_error('CONFIG > Invalid configuration. %s' % json_to_str(appdata))
            # Update modified job
            elif old_appdata['version'] != appdata['version']:
                for scheduled_job in g_sched.get_jobs():
                    if scheduled_job.name == appdata['name']:
                        if verifyAppdata(appdata):
                            log_info('CONFIG > update job %s' % json_to_str(appdata))
                            g_sched.unschedule_job(scheduled_job)
                            g_sched.add_cron_job(backup, name=appdata['name'], args=[appdata], 
                                    minute=mi, hour=h, day=d, month=mo, day_of_week=day_of_week, year=y)
                            changed = True
                        else:
                            log_error('CONFIG > Invalid configuration. %s' % json_to_str(appdata))
                        break
        except ValueError as e:
            log_error('CONFIG > ValueError: %s' % e)
            continue
        except KeyError as e:
            log_error('CONFIG > KeyError: %s' % e)
            continue
        except:
            log_exception("CONFIG > ")
            continue

    g_old_appdata = appdata_list

    # Handle deleted jobs
    deleted_jobs = g_sched.get_jobs()
    for sched_job in g_sched.get_jobs():
        if sched_job.name == 'update_jobs': deleted_jobs.remove(sched_job)

        job = find_in_list_of_dicts(appdata_list, "name", sched_job.name)
        if len(job) != 0:
            deleted_jobs.remove(sched_job)

    for deleted_job in deleted_jobs:
        changed = True
        log_info('CONFIG > delete job : %s' % deleted_job)
        g_sched.unschedule_job(deleted_job)

    if changed == True:
        print_scheduled_jobs()
    else:
        # Print backup jobs
        g_update_jobs_cnt += 1
        if g_update_jobs_cnt >= 10:
            print_scheduled_jobs()
            g_update_jobs_cnt = 0
Esempio n. 4
0
def update_jobs():
    global g_update_jobs_cnt
    global g_sched
    global g_old_appdata

    log_info('running...')

    out_cluster_ls = cm.cluster_ls()
    if out_cluster_ls.cm == False:
        log_info("CONFIG > mgmt-cc error.")
        return

    # Get jobs from CM
    appdata_list = []
    for cluster_name in out_cluster_ls.json['data']['list']:
        # Appdata contains job of backup
        out = cm.appdata_get(cluster_name.encode('ascii'))
        if out.cm == False:
            log_info("CONFIG > mgmt-cc error.")
            return

        if out.appdata_list == None:
            log_info("CONFIG > Cluster '%s' doesn't have any appdata" %
                     (cluster_name))
        else:
            for data in out.appdata_list:
                appdata_list.append(data)

    # Update jobs
    changed = False
    for appdata in appdata_list:
        period = appdata['period'].split(' ')
        y = period[5]
        day_of_week = period[4]
        mo = period[3]
        d = period[2]
        h = period[1]
        mi = period[0]

        old_appdata = find_in_list_of_dicts(g_old_appdata, "name",
                                            appdata["name"])

        if len(old_appdata) == 0:
            old_appdata = None
        else:
            old_appdata = old_appdata[0]

        try:
            # Add new job
            if old_appdata == None:
                sort_keys = True,
                log_info('CONFIG > add job : %s' % json_to_str(appdata))
                if h == 24:
                    h = 0

                if verifyAppdata(appdata):
                    g_sched.add_cron_job(backup,
                                         name=appdata['name'],
                                         args=[appdata],
                                         minute=mi,
                                         hour=h,
                                         day=d,
                                         month=mo,
                                         day_of_week=day_of_week,
                                         year=y)
                    changed = True
                else:
                    log_error('CONFIG > Invalid configuration. %s' %
                              json_to_str(appdata))
            # Update modified job
            elif old_appdata['version'] != appdata['version']:
                for scheduled_job in g_sched.get_jobs():
                    if scheduled_job.name == appdata['name']:
                        if verifyAppdata(appdata):
                            log_info('CONFIG > update job %s' %
                                     json_to_str(appdata))
                            g_sched.unschedule_job(scheduled_job)
                            g_sched.add_cron_job(backup,
                                                 name=appdata['name'],
                                                 args=[appdata],
                                                 minute=mi,
                                                 hour=h,
                                                 day=d,
                                                 month=mo,
                                                 day_of_week=day_of_week,
                                                 year=y)
                            changed = True
                        else:
                            log_error('CONFIG > Invalid configuration. %s' %
                                      json_to_str(appdata))
                        break
        except ValueError as e:
            log_error('CONFIG > ValueError: %s' % e)
            continue
        except KeyError as e:
            log_error('CONFIG > KeyError: %s' % e)
            continue
        except:
            log_exception("CONFIG > ")
            continue

    g_old_appdata = appdata_list

    # Handle deleted jobs
    deleted_jobs = g_sched.get_jobs()
    for sched_job in g_sched.get_jobs():
        if sched_job.name == 'update_jobs': deleted_jobs.remove(sched_job)

        job = find_in_list_of_dicts(appdata_list, "name", sched_job.name)
        if len(job) != 0:
            deleted_jobs.remove(sched_job)

    for deleted_job in deleted_jobs:
        changed = True
        log_info('CONFIG > delete job : %s' % deleted_job)
        g_sched.unschedule_job(deleted_job)

    if changed == True:
        print_scheduled_jobs()
    else:
        # Print backup jobs
        g_update_jobs_cnt += 1
        if g_update_jobs_cnt >= 10:
            print_scheduled_jobs()
            g_update_jobs_cnt = 0