def upload_sys_info(request):
    return_dict = {}
    try:
        if request.method == "POST":
            status, path = _handle_uploaded_file(request.FILES['file_field'])
            display_name, err = config.get_config_dir()
            if err:
                raise Exception(err)
            if path:
                zip = zipfile.ZipFile(path, 'r')
                data = zip.namelist()
                move = zip.extractall("/tmp/upload/")
               # logs = {'smb_conf':'/etc/samba/smb.conf','ntp_conf':'/etc/ntp.conf','krb5_conf':'/etc/krb5.conf','nfs':'/etc/exports','ftp':'/etc/vsftpd/vsftpd.conf'}
                logs = {'smb_conf': '/etc/samba/smb.conf', 'ntp_conf': '/etc/ntp.conf', 'krb5_conf': '/etc/krb5.conf', 'nfs': '/etc/exports',
                        'ftp': '/etc/vsftpd/vsftpd.conf', 'master.status': display_name + "/status/master.status", 'master.manifest': display_name + "/status/master.manifest"}
                for key, value in logs.iteritems():
                    if key and os.path.isfile("/tmp/upload/" + key):
                        _copy_file_and_overwrite("/tmp/upload/" + key, value)
                for dir in os.listdir("/tmp/upload"):
                    if dir and os.path.isdir("/tmp/upload/" + dir):
                        _copy_and_overwrite(
                            "/tmp/upload/" + dir, config.get_config_dir()[0] + "/" + dir)
                return django.http.HttpResponseRedirect("/view_system_info/")
        else:
            form = common_forms.FileUploadForm()
            return_dict["form"] = form
            return django.shortcuts.render_to_response("upload_sys_info.html", return_dict, context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        return_dict['base_template'] = "logging_base.html"
        return_dict["error"] = 'Error displaying rotated log list'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def add_to_nodes_file(ip_list):
    """
    Append a set of IP addresses to the CTDB nodes file in admin vol.

    ip_list -- A list of IP addresses to be appended
    """
    existing_ips = []
    try:
        if not ip_list:
            raise Exception("No IPs to add to the CTDB nodes file!")

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        try:
            with open('%s/lock/nodes' % config_dir, 'r') as f:
                for line in f:
                    existing_ips.append(line.strip())
        except Exception, e:
            # In case there is no file existing, go ahead
            pass

        with open('%s/lock/nodes' % config_dir, 'a') as f:
            for ip in ip_list:
                if existing_ips:
                    if ip.strip() not in existing_ips:
                        f.write("%s\n" % ip)
                else:
                    f.write("%s\n" % ip)
def add_to_nodes_file(ip_list):
    """
    Append a set of IP addresses to the CTDB nodes file in admin vol.

    ip_list -- A list of IP addresses to be appended
    """
    existing_ips = []
    try:
        if not ip_list:
            raise Exception("No IPs to add to the CTDB nodes file!")

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        try:
            with open('%s/lock/nodes' % config_dir, 'r') as f:
                for line in f:
                    existing_ips.append(line.strip())
        except Exception, e:
            # In case there is no file existing, go ahead
            pass

        with open('%s/lock/nodes' % config_dir, 'a') as f:
            for ip in ip_list:
                if existing_ips:
                    if ip.strip() not in existing_ips:
                        f.write("%s\n" % ip)
                else:
                    f.write("%s\n" % ip)
def get_public_addresses():
    """
    Return IP addresses from CTDB public_addresses file in admin vol.

    Input:
      ['None']
    Output:
      [ip_list, 'None'] on successful execution
      ['None', 'Error string'] on failure/exception
    """

    ip_list = []
    iface = "bond0"
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        file_name = '%s/lock/public_addresses' % config_dir
        try:
            with open(file_name, 'r') as f:
                for line in f:
                    ip_list.append(line.strip())
            f.close()
        except Exception, e:
            # In case there is no file existing, go ahead
            pass

    except Exception, e:
        return False, "Error reading from CTDB public_addresses file : %s" % str(e)
Exemple #5
0
def add_to_db(dir, action, file=None):
    try:
        if file:
            path = '%s/%s' % (dir, file)
        else:
            path = dir

        path = os.path.normpath(path)

        tz = pytz.timezone('UTC')
        now_utc = datetime.datetime.now(tz)

        time = now_utc.strftime('%Y-%m-%d %H:%M:%S')

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.execute_iud(db_path, [[
            "insert into logs (path, actions, timestamp) values (?, ?, ?)",
            (
                path,
                action,
                time,
            )
        ]])
        if err:
            print err

    except Exception, e:
        return False, 'Error inserting into database dir - %s, action - %s, file = %s : %s' % (
            dir, action, file, str(e))
def add_to_db(dir, action, file=None):
    try:
        if file:
            path = '%s/%s' % (dir, file)
        else:
            path = dir

        path = os.path.normpath(path)

        tz = pytz.timezone('UTC')
        now_utc = datetime.datetime.now(tz)

        time = now_utc.strftime('%Y-%m-%d %H:%M:%S')

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.execute_iud(db_path, [
                                  ["insert into logs (path, actions, timestamp) values (?, ?, ?)", (path, action, time,)]])
        if err:
            print err

    except Exception, e:
        return False, 'Error inserting into database dir - %s, action - %s, file = %s : %s' % (dir, action, file, str(e))
def get_public_addresses():
    """
    Return IP addresses from CTDB public_addresses file in admin vol.

    Input:
      ['None']
    Output:
      [ip_list, 'None'] on successful execution
      ['None', 'Error string'] on failure/exception
    """

    ip_list = []
    iface = "bond0"
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        file_name = '%s/lock/public_addresses' % config_dir
        try:
            with open(file_name, 'r') as f:
                for line in f:
                    ip_list.append(line.strip())
            f.close()
        except Exception, e:
            # In case there is no file existing, go ahead
            pass

    except Exception, e:
        return False, "Error reading from CTDB public_addresses file : %s" % str(
            e)
def mod_public_addresses(ip_list, action="add"):
    """
    Append a set of IP addresses to CTDB public_addresses in admin vol.

    Input:
      ip_list -- A list of IP addresses(with prefix length, i.e, in CIDR form)
      action -- add, remove, create
    Output:
      ['True', 'None'] on successful execution
      ['None', 'Error string'] on failure/exception
    """

    existing_ips = []
    iface = "bond0"
    try:
        if action != "create" and not ip_list:
            raise Exception(
                "No IPs provided to add to the CTDB public_addresses file!")

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        file_name = '%s/lock/public_addresses' % config_dir
        existing_ips, err = get_public_addresses()
        if err:
            raise Exception(err)

        if action not in ['add', 'remove', 'create']:
            raise Exception(
                "Unrecognized action: %s. [only 'add' & 'remove' permitted]")
        if action == "add":
            with open(file_name, 'a') as f:
                for ip in ip_list:
                    ip_iface = "%s %s" % (ip, iface)
                    if existing_ips:
                        if ip_iface not in existing_ips:
                            f.write("%s\n" % ip_iface)
                    else:
                        f.write("%s\n" % ip_iface)
            f.close()
        elif action == 'remove':
            with open(file_name, 'w') as f:
                for remov in ip_list:
                    remov_ip = "%s %s" % (remov, iface)
                    print remov_ip
                    print existing_ips
                    if existing_ips:
                        existing_ips.remove(remov_ip)
                if existing_ips:
                    for ip in existing_ips:
                        f.write("%s\n" % ip)
            f.close()
        elif action == 'create':
            with open(file_name, 'w') as f:
                pass
            f.close()
    except Exception, e:
        return False, "Error editing CTDB public_addresses file : %s" % str(e)
def mod_public_addresses(ip_list, action="add"):
    """
    Append a set of IP addresses to CTDB public_addresses in admin vol.

    Input:
      ip_list -- A list of IP addresses(with prefix length, i.e, in CIDR form)
      action -- add, remove, create
    Output:
      ['True', 'None'] on successful execution
      ['None', 'Error string'] on failure/exception
    """

    existing_ips = []
    iface = "bond0"
    try:
        if action != "create" and not ip_list:
            raise Exception(
                "No IPs provided to add to the CTDB public_addresses file!")

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        file_name = '%s/lock/public_addresses' % config_dir
        existing_ips, err = get_public_addresses()
        if err:
            raise Exception(err)

        if action not in ['add', 'remove', 'create']:
            raise Exception(
                "Unrecognized action: %s. [only 'add' & 'remove' permitted]")
        if action == "add":
            with open(file_name, 'a') as f:
                for ip in ip_list:
                    ip_iface = "%s %s" % (ip, iface)
                    if existing_ips:
                        if ip_iface not in existing_ips:
                            f.write("%s\n" % ip_iface)
                    else:
                        f.write("%s\n" % ip_iface)
            f.close()
        elif action == 'remove':
            with open(file_name, 'w') as f:
                for remov in ip_list:
                    remov_ip = "%s %s" % (remov, iface)
                    print remov_ip
                    print existing_ips
                    if existing_ips:
                        existing_ips.remove(remov_ip)
                if existing_ips:
                    for ip in existing_ips:
                        f.write("%s\n" % ip)
            f.close()
        elif action == 'create':
            with open(file_name, 'w') as f:
                pass
            f.close()
    except Exception, e:
        return False, "Error editing CTDB public_addresses file : %s" % str(e)
Exemple #10
0
def generate_smb_conf():
    try:
        d, err = cifs_common.get_auth_settings()
        if err:
            raise Exception(err)
        smb_conf_path, err = config.get_smb_conf_path()
        if err:
            raise Exception(err)
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        # For customer specific or non configurable smb.conf lines
        extra_share_param_lines = None
        extra_global_param_lines = None
        if os.path.isfile('%s/customer_specific/extra_smb_share_params.conf' %
                          config_dir):
            with open(
                    '%s/customer_specific/extra_smb_share_params.conf' %
                    config_dir, 'r') as f1:
                extra_share_param_lines = f1.readlines()
        if os.path.isfile('%s/customer_specific/extra_smb_global_params.conf' %
                          config_dir):
            with open(
                    '%s/customer_specific/extra_smb_global_params.conf' %
                    config_dir, 'r') as f1:
                extra_global_param_lines = f1.readlines()
        # print extra_share_param_lines
        # print extra_global_param_lines
        with open("%s/smb.conf" % smb_conf_path, "w+") as f:
            ret, err = cifs_common.generate_global_header(f)
            if err:
                raise Exception(err)
            ret, err = _generate_integralstor_specific_global_section(f, d)
            if err:
                raise Exception(err)
            ret, err = cifs_common.generate_common_global_section(
                f, d, extra_global_param_lines)
            if err:
                raise Exception(err)
            shl, err = cifs_common.get_shares_list()
            if err:
                raise Exception(err)
            if shl:
                for share in shl:
                    ret, err = _generate_share_section(
                        f, share["name"], d["workgroup"], share["path"],
                        share["read_only"], share["browseable"],
                        share["comment"], d["security"],
                        extra_share_param_lines, share['hosts_allow'],
                        share['hosts_deny'])
                    if err:
                        raise Exception(err)
        ret, errors = reload_configuration()
        if errors:
            raise Exception(errors)
    except Exception, e:
        return False, 'Error generating CIFS configuration : %s' % str(e)
def download_system_configuration(request):
    """ Download the complete configuration stored in get_config_dir()"""

    return_dict = {}
    try:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'Download system configuration'
        return_dict['tab'] = 'download_config_tab'
        return_dict["error"] = 'Error downloading system configuration'

        if request.method == 'POST':
            config_dir, err = config.get_config_dir()
            if err:
                raise Exception(err)
            # Remove trailing '/'
            if config_dir[len(config_dir) - 1] == '/':
                config_dir = config_dir[:len(config_dir) - 1]

            display_name = 'integralstor_config'
            zf_name = '/tmp/integralstor_config.zip'
            zf = zipfile.ZipFile(zf_name, 'w')
            top_component = config_dir[config_dir.rfind('/') + 1:]
            for dirname, subdirs, files in os.walk(config_dir):
                for filename in files:
                    # print os.path.join(dirname, filename)
                    absname = os.path.abspath(os.path.join(dirname, filename))
                    arcname = '%s/%s' % (top_component,
                                         absname[len(config_dir) + 1:])
                    # print arcname
                    zf.write(absname, arcname)
            zf.close()

            response = django.http.HttpResponse()
            response['Content-disposition'] = 'attachment; filename=%s.zip' % (
                display_name)
            response['Content-type'] = 'application/x-compressed'
            with open(zf_name, 'rb') as f:
                byte = f.read(1)
                while byte:
                    response.write(byte)
                    byte = f.read(1)
            response.flush()

            return response

        # either a get or an invalid form so send back form
        return django.shortcuts.render_to_response('download_system_configuration.html', return_dict, context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        s = str(e)
        if "Another transaction is in progress".lower() in s.lower():
            return_dict["error"] = "An underlying storage operation has locked a volume so we are unable to process this request. Please try after a couple of seconds"
        else:
            return_dict["error"] = "An error occurred when processing your request : %s" % s
        return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
Exemple #12
0
def remove_old_entries(older_than=43200):
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        query = "delete from logs where timestamp < Datetime('now', '-%d seconds');" % older_than
        # print query
        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.execute_iud(db_path, [[query]])
        if err:
            print err
        # print ret
    except Exception, e:
        return False, 'Error purging old entries : %s' % str(e)
def remove_old_entries(older_than=43200):
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        query = "delete from logs where timestamp < Datetime('now', '-%d seconds');" % older_than
        # print query
        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.execute_iud(db_path, [[query]])
        if err:
            print err
        # print ret
    except Exception, e:
        return False, 'Error purging old entries : %s' % str(e)
Exemple #14
0
def auto_rotate_logs():
    """Called from a script to rotate the audit and alert logs."""
    try:
        config, err = config.get_config_dir()
        if err:
            raise Exception(err)
        # Takes the log dir inside the config/logs and the log file name and
        # auto-rotates it.
        log_dict = {'audit': 'audit.log', 'alerts': 'alerts.log'}
        for key, value in log_dict.items():
            status, err = rotate_log(
                config + "/logs/" + key + "/", value, None)
            if err:
                raise Exception(err)
    except Exception, e:
        return False, "Error rotating logs " + str(e)
Exemple #15
0
def auto_rotate_logs():
    """Called from a script to rotate the audit and alert logs."""
    try:
        config, err = config.get_config_dir()
        if err:
            raise Exception(err)
        # Takes the log dir inside the config/logs and the log file name and
        # auto-rotates it.
        log_dict = {'audit': 'audit.log', 'alerts': 'alerts.log'}
        for key, value in log_dict.items():
            status, err = rotate_log(config + "/logs/" + key + "/", value,
                                     None)
            if err:
                raise Exception(err)
    except Exception, e:
        return False, "Error rotating logs " + str(e)
Exemple #16
0
def zip_grid_gluster_config():
    zf = None
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        zf = zipfile.ZipFile('/tmp/grid_gluster_config.zip',
                             'w', zipfile.ZIP_DEFLATED)
        for root, dirs, files in os.walk('%s/config_backup/gridcells' % config_dir):
            for file in files:
                # print file
                zf.write(os.path.join(root, file), file)
        zf.close()
        if not os.path.exists('%s/config_backup/grid' % log_dir):
            os.makedirs('%s/config_backup/grid' % log_dir)
        shutil.move('/tmp/grid_gluster_config.zip',
                    '%s/config_backup/grid/gluster_config.zip' % log_dir)
    except Exception, e:
        return False, 'Error zipping GRID gluster config : %s' % str(e)
def download_sys_info(request):
    return_dict = {}
    try:
        display_name, err = config.get_config_dir()
        if err:
            raise Exception(err)
        zf_name = "system_info.zip"
        try:
            zf = zipfile.ZipFile(zf_name, 'w')
            abs_src = os.path.abspath(display_name)
            for dirname, subdirs, files in os.walk(display_name):
                if not(("logs" in dirname) or ("ntp" in dirname) or ("rc_local" in dirname) or ("samba" in dirname) or ("status" in dirname)):
                    for filename in files:
                        absname = os.path.abspath(
                            os.path.join(dirname, filename))
                        arcname = absname[len(abs_src) + 1:]
                        zf.write(absname, arcname)
            logs = {'smb_conf': '/etc/samba/smb.conf', 'ntp_conf': '/etc/ntp.conf', 'krb5_conf': '/etc/krb5.conf', 'nfs': '/etc/exports',
                    'ftp': '/etc/vsftpd/vsftpd.conf', 'master.status': display_name + "/status/master.status", 'master.manifest': display_name + "/status/master.manifest"}
            for key, value in logs.iteritems():
                if os.path.isfile(value):
                    zf.write(value, key)
            zf.close()
        except Exception as e:
            raise Exception("Error compressing remote log file : %s" % str(e))
        response = django.http.HttpResponse()
        response['Content-disposition'] = 'attachment; filename=system_info.zip'
        response['Content-type'] = 'application/x-compressed'
        with open(zf_name, 'rb') as f:
            byte = f.read(1)
            while byte:
                response.write(byte)
                byte = f.read(1)
        response.flush()
        return response
    except Exception as e:
        return_dict['base_template'] = "logging_base.html"
        return_dict["page_title"] = 'Download system information'
        return_dict['tab'] = 'node_info_tab'
        return_dict["error"] = 'Error downloading system information'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def create_config_file():
    """Create the CTDB configuration file pointing to the required files."""

    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        with open("/etc/sysconfig/ctdb", "w") as f:
            f.write("CTDB_RECOVERY_LOCK=%s/lock/ctdb_rec_lockfile\n" %
                    config_dir)
            f.write("CTDB_NODES=/etc/ctdb/nodes\n")
            f.write("CTDB_PUBLIC_ADDRESSES=/etc/ctdb/public_addresses\n")
            f.write("CTDB_MANAGES_SAMBA=yes\n")
            f.write("CTDB_MANAGES_WINBIND=yes\n")
            f.write("CTDB_SAMBA_SKIP_SHARE_CHECK=yes\n")
            f.write("CTDB_DEBUGLEVEL=NOTICE\n")
            f.write("CTDB_SET_RecoveryBanPeriod=150\n")
            f.close()
    except Exception, e:
        return False, "Error creating ctdb config file : %s" % str(e)
def create_config_file():
    """Create the CTDB configuration file pointing to the required files."""

    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        with open("/etc/sysconfig/ctdb", "w") as f:
            f.write("CTDB_RECOVERY_LOCK=%s/lock/ctdb_rec_lockfile\n" %
                    config_dir)
            f.write("CTDB_NODES=/etc/ctdb/nodes\n")
            f.write("CTDB_PUBLIC_ADDRESSES=/etc/ctdb/public_addresses\n")
            f.write("CTDB_MANAGES_SAMBA=yes\n")
            f.write("CTDB_MANAGES_WINBIND=yes\n")
            f.write("CTDB_SAMBA_SKIP_SHARE_CHECK=yes\n")
            f.write("CTDB_DEBUGLEVEL=NOTICE\n")
            f.write("CTDB_SET_RecoveryBanPeriod=150\n")
            f.close()
    except Exception, e:
        return False, "Error creating ctdb config file : %s" % str(e)
def assert_admin_vol_mount():
    try:
        # Check if the admin vol mount actually succeded by reading a file from the admin volume
        # Tries 3 times. Bail out if unsuccessfull
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        iter = 3
        while (iter):
            iter -= 1
            is_mounted, err = grid_ops.is_admin_vol_mounted_local()
            if err or (is_mounted is False):
                time.sleep(5)
            elif is_mounted is True:
                break
        if iter < 1:
            raise Exception("Admin volume is not mounted!")

    except Exception, e:
        return False, "Couldn't assert mount point access: %s" % str(e)
def assert_admin_vol_mount():
    try:
        # Check if the admin vol mount actually succeded by reading a file from the admin volume
        # Tries 3 times. Bail out if unsuccessfull
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        iter = 3
        while (iter):
            iter -= 1
            is_mounted, err = grid_ops.is_admin_vol_mounted_local()
            if err or (is_mounted is False):
                time.sleep(5)
            elif is_mounted is True:
                break
        if iter < 1:
            raise Exception("Admin volume is not mounted!")

    except Exception, e:
        return False, "Couldn't assert mount point access: %s" % str(e)
def get_count(action='READ', past_x_seconds=60):
    count = -1
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        if action != 'ALL':
            query = "select count(*) as count from logs where timestamp >= Datetime('now', '-%d seconds');" % past_x_seconds
        else:
            query = "select count(*) as count from logs where actions = '%s' and timestamp >= Datetime('now', '-%d seconds');" % (
                action.upper(), past_x_seconds)
        # print query
        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.get_single_row(db_path, query)
        if err:
            print err
        count = ret['count']
        # print ret
    except Exception, e:
        return -1, 'Error getting counts : %s' % str(e)
Exemple #23
0
def get_count(action='READ', past_x_seconds=60):
    count = -1
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        if action != 'ALL':
            query = "select count(*) as count from logs where timestamp >= Datetime('now', '-%d seconds');" % past_x_seconds
        else:
            query = "select count(*) as count from logs where actions = '%s' and timestamp >= Datetime('now', '-%d seconds');" % (
                action.upper(), past_x_seconds)
        # print query
        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.get_single_row(db_path, query)
        if err:
            print err
        count = ret['count']
        # print ret
    except Exception, e:
        return -1, 'Error getting counts : %s' % str(e)
Exemple #24
0
def zip_gridcell_gluster_config():
    zf = None
    try:
        hn = socket.getfqdn()
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        zf = zipfile.ZipFile('/tmp/gluster_config_%s.zip' %
                             hn, 'w', zipfile.ZIP_DEFLATED)
        for root, dirs, files in os.walk('/var/lib/glusterd'):
            for file in files:
                # print file
                zf.write(os.path.join(root, file))
        zf.close()
        if not os.path.exists('%s/config_backup/gridcells/' % config_dir):
            os.makedirs('%s/config_backup/gridcells/' % config_dir)
        shutil.move('/tmp/gluster_config_%s.zip' %
                    hn, '%s/config_backup/gridcells/%s.zip' % (config_dir, hn))
    except Exception, e:
        return False, 'Error zipping GRIDCell gluster configuration : %s' % str(e)
def check_hosts_file_entries(client=None):
    try:
        if not client:
            client = salt.client.LocalClient()

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        with open('%s/master_hosts_file' % config_dir, 'r') as f:
            master_lines = f.readlines()

        current_entries = client.cmd('*', 'hosts.list_hosts')

        if not current_entries:
            raise Exception('Error retrieving current host entries')

        errors = []
        for host, entries in current_entries.items():
            current_ips = entries.keys()
            for line in master_lines:
                components = line.strip().split()
                ip = components[0].strip()
                hn = components[1].strip()
                if len(components) != 2:
                    raise Exception('Incorrect master hosts entry : %s' % line)
                if ip not in current_ips:
                    errors.append(
                        'The IP address %s is not present in GRIDCell %s' % (ip, host))
                elif hn not in entries[ip]:
                    errors.append('The hostname %s for IP address %s is not present in GRIDCell %s. The erroneous hosts are "%s"' % (
                        hn, ip, host, ','.join(entries[ip])))
        if not errors:
            print 'all ok'
        else:
            print errors

    except Exception, e:
        return False, "Error checking hosts file entries : %s" % str(e)
def remove_from_nodes_file(ip_list):
    """ Remove a (set of) IP addresses from the CTDB nodes file

    ip_list -- A list of IP addresses to be removed
    """

    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        with open('/tmp/ctdb_nodes_file', 'w') as f1:
            with open('%s/lock/nodes' % config_dir, 'r') as f:
                for line in f:
                    if line.strip() in ip_list:
                        continue
                    else:
                        f1.write('%s\n' % line.strip())
        shutil.move('/tmp/ctdb_nodes_file', '%s/lock/nodes' % config_dir)

    except Exception, e:
        return False, "Error removing IPs from the CTDB Nodes file : %s" % str(e)
def _generate_gridcell_specific_global_section(f, d):
    """Generate the global section of smb.conf that is specific to gridcell

    f -- The file handle for the smb.conf file
    d -- a dictionary which contains the relavant details needed to create that section

    Returns True on success and False otherwise
    """
    try:
        if not f:
            raise Exception('No file handle passed')

        conf_path, err = config.get_config_dir()
        if err:
            raise Exception(err)

        f.write("  server string = IntegralStor GRIDCell File server\n")
        f.write("  netbios name = %s\n" % d["netbios_name"].upper())
        # Commenting this out as we wont use CTDB for this release
        #f.write("  clustering=yes\n")
        f.write("  private dir=%s/lock\n" % conf_path)
    except Exception, e:
        return False, 'Error generating GRIDCell CIFS configuration - global section : %s' % str(e)
def remove_from_nodes_file(ip_list):
    """ Remove a (set of) IP addresses from the CTDB nodes file

    ip_list -- A list of IP addresses to be removed
    """

    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        with open('/tmp/ctdb_nodes_file', 'w') as f1:
            with open('%s/lock/nodes' % config_dir, 'r') as f:
                for line in f:
                    if line.strip() in ip_list:
                        continue
                    else:
                        f1.write('%s\n' % line.strip())
        shutil.move('/tmp/ctdb_nodes_file', '%s/lock/nodes' % config_dir)

    except Exception, e:
        return False, "Error removing IPs from the CTDB Nodes file : %s" % str(
            e)
def _generate_gridcell_specific_global_section(f, d):
    """Generate the global section of smb.conf that is specific to gridcell

    f -- The file handle for the smb.conf file
    d -- a dictionary which contains the relavant details needed to create that section

    Returns True on success and False otherwise
    """
    try:
        if not f:
            raise Exception('No file handle passed')

        conf_path, err = config.get_config_dir()
        if err:
            raise Exception(err)

        f.write("  server string = IntegralStor GRIDCell File server\n")
        f.write("  netbios name = %s\n" % d["netbios_name"].upper())
        # Commenting this out as we wont use CTDB for this release
        #f.write("  clustering=yes\n")
        f.write("  private dir=%s/lock\n" % conf_path)
    except Exception, e:
        return False, 'Error generating GRIDCell CIFS configuration - global section : %s' % str(
            e)
def generate_smb_conf():
    """Generate the complete of smb.conf that is specific to gridcell

    Returns True on success and False otherwise
    """
    try:
        d, err = cifs_utils.get_auth_settings()
        if err:
            raise Exception(err)

        smb_conf_path, err = config.get_smb_conf_path()
        if err:
            raise Exception(err)

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        # For customer specific or non configurable smb.conf lines
        extra_share_param_lines = None
        extra_global_param_lines = None
        if os.path.isfile('%s/customer_specific/extra_smb_share_params.conf' %
                          config_dir):
            with open(
                    '%s/customer_specific/extra_smb_share_params.conf' %
                    config_dir, 'r') as f1:
                extra_share_param_lines = f1.readlines()
        if os.path.isfile('%s/customer_specific/extra_smb_global_params.conf' %
                          config_dir):
            with open(
                    '%s/customer_specific/extra_smb_global_params.conf' %
                    config_dir, 'r') as f1:
                extra_global_param_lines = f1.readlines()
        # print extra_share_param_lines
        # print extra_global_param_lines

        with open("%s/smb.conf" % smb_conf_path, "w+") as f:
            ret, err = cifs_utils.generate_global_header(f)
            if err:
                raise Exception(err)

            ret, err = _generate_gridcell_specific_global_section(f, d)
            if err:
                raise Exception(err)

            ret, err = cifs_utils.generate_common_global_section(
                f, d, extra_global_param_lines)
            if err:
                raise Exception(err)

            ret, err = _generate_local_share(f)
            if err:
                raise Exception(err)

            shl, err = cifs_utils.get_shares_list()
            if err:
                raise Exception(err)

            if shl:
                for share in shl:
                    ul = []
                    gl = []
                    if not share["guest_ok"]:
                        vul, err = get_valid_users_list(share["share_id"])
                        if err:
                            raise Exception(err)
                        # print 'vul is ', vul
                        if vul:
                            for vu in vul:
                                if vu["grp"]:
                                    gl.append(vu["name"])
                                else:
                                    ul.append(vu["name"])
                    share_dict = {}
                    share_dict['share_name'] = share['name']
                    share_dict['vol_name'] = share['vol']
                    share_dict['workgroup'] = d['workgroup']
                    share_dict['path'] = share['path']
                    share_dict['read_only'] = share['read_only']
                    share_dict['browseable'] = share['browseable']
                    share_dict['guest_ok'] = share['guest_ok']
                    share_dict['comment'] = share['comment']
                    share_dict['auth_method'] = d['security']
                    share_dict['user_list'] = ul
                    share_dict['group_list'] = gl
                    ret, err = _generate_share_section(
                        f, share_dict, extra_share_param_lines)
                    if err:
                        raise Exception(err)

        ret, errors = _reload_config()
        if errors:
            raise Exception(errors)

    except Exception, e:
        return False, 'Error generating CIFS configuration :%s' % str(e)
def edit_ntp_settings(request):

    return_dict = {}
    try:
        return_dict['base_template'] = "services_base.html"
        return_dict["page_title"] = 'Configure NTP settings'
        return_dict['tab'] = 'service_ntp_tab'
        return_dict["error"] = 'Error configuring NTP settings'

        admin_vol_mountpoint, err = config.get_config_dir()
        if err:
            raise Exception(err)
        if err:
            raise Exception(err)
        if request.method == "GET":
            ntp_servers, err = ntp.get_ntp_servers()
            if err:
                raise Exception(err)
            if not ntp_servers:
                form = common_forms.ConfigureNTPForm()
            else:
                form = common_forms.ConfigureNTPForm(
                    initial={'server_list': ','.join(ntp_servers)})
            url = "edit_ntp_settings.html"
        else:
            form = common_forms.ConfigureNTPForm(request.POST)
            if form.is_valid():
                #iv_logging.debug("Got valid request to change NTP settings")
                cd = form.cleaned_data
                si, err = system_info.load_system_config()
                if err:
                    raise Exception(err)
                if not si:
                    raise Exception(
                        'Error loading system configuration. System config missing?'
                    )
                server_list = cd["server_list"]
                if ',' in server_list:
                    slist = server_list.split(',')
                else:
                    slist = server_list.split(' ')
                primary_server = "gridcell-pri.integralstor.lan"
                secondary_server = "gridcell-sec.integralstor.lan"
                # First create the ntp.conf file for the primary and secondary
                # nodes
                temp = tempfile.NamedTemporaryFile(mode="w")
                temp.write("driftfile /var/lib/ntp/drift\n")
                temp.write(
                    "restrict default kod nomodify notrap nopeer noquery\n")
                temp.write(
                    "restrict -6 default kod nomodify notrap nopeer noquery\n")
                temp.write("includefile /etc/ntp/crypto/pw\n")
                temp.write("keys /etc/ntp/keys\n")
                temp.write("\n")
                for server in slist:
                    temp.write("server %s iburst\n" % server)
                temp.flush()
                shutil.move(temp.name,
                            "%s/ntp/primary_ntp.conf" % admin_vol_mountpoint)
                temp1 = tempfile.NamedTemporaryFile(mode="w")
                temp1.write("server %s iburst\n" % primary_server)
                temp1.write("server %s iburst\n" % secondary_server)
                for s in si.keys():
                    temp1.write("peer %s iburst\n" % s)
                temp1.write("server 127.127.1.0\n")
                temp1.write("fudge 127.127.1.0 stratum 10\n")
                temp1.flush()
                shutil.move(temp1.name,
                            "%s/ntp/secondary_ntp.conf" % admin_vol_mountpoint)
                return django.http.HttpResponseRedirect(
                    "/view_ntp_settings?saved=1")
            else:
                # invalid form
                #iv_logging.debug("Got invalid request to change NTP settings")
                url = "edit_ntp_settings.html"
        return_dict["form"] = form
        return django.shortcuts.render_to_response(
            url,
            return_dict,
            context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        s = str(e)
        if "Another transaction is in progress".lower() in s.lower():
            return_dict[
                "error_details"] = "An underlying storage operation has locked a volume so we are unable to process this request. Please try after a couple of seconds"
        else:
            return_dict[
                "error_details"] = "An error occurred when processing your request : %s" % s
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def sync_ctdb_files():
    """
    Syncs CTDB files on the localhost with the mounted admin vol.

    Input:
      ['None']
    Output:
      Returns three variables [ret1,ret2,ret3]:
        ret1 -- True if sync was sucessful, else False
        ret2 -- True if there was a difference, False if they were already in sync
        ret3 -- None if there were no errors/exceptions, 'Error string' otherwise
    """

    is_change = False
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        out, err = command.get_command_output(
            'diff "/etc/sysconfig/ctdb" "%s/lock/ctdb"' % str(config_dir),
            True, True)
        if err:
            shutil.copyfile('%s/lock/ctdb' % str(config_dir),
                            '/etc/sysconfig/ctdb')
            is_change = True

        out, err = command.get_command_output(
            'diff "/etc/ctdb/nodes" "%s/lock/nodes"' % str(config_dir), True,
            True)
        if err:
            shutil.copyfile('%s/lock/nodes' % str(config_dir),
                            '/etc/ctdb/nodes')
            is_change = True

        out, err = command.get_command_output(
            'diff "/etc/ctdb/public_addresses" "%s/lock/public_addresses"' %
            str(config_dir), True, True)
        if err:
            shutil.copyfile('%s/lock/public_addresses' % str(config_dir),
                            '/etc/ctdb/public_addresses')
            is_change = True

        lg, err = logger.get_script_logger(
            'Admin volume mounter: Sync CTDB config files',
            '/var/log/integralstor/scripts.log',
            level=logging.DEBUG)
        if is_change == True:
            logger.log_or_print('ctdb related files were synced.',
                                lg,
                                level='info')
            logger.log_or_print('Restarting ctdb.', lg, level='debug')
            out, err = command.get_command_output('service ctdb restart',
                                                  False, True)
            if not err and out:
                logger.log_or_print('Service ctdb: %s' % out,
                                    lg,
                                    level='debug')
            else:
                logger.log_or_print('Service ctdb error: %s' % err,
                                    lg,
                                    level='error')
        elif is_change == False:
            logger.log_or_print('ctdb related files are in sync.',
                                lg,
                                level='info')

    except Exception, e:
        return False, is_change, "Couldn't sync ctdb files: %s" % str(e)
def sync_ctdb_files():
    """
    Syncs CTDB files on the localhost with the mounted admin vol.

    Input:
      ['None']
    Output:
      Returns three variables [ret1,ret2,ret3]:
        ret1 -- True if sync was sucessful, else False
        ret2 -- True if there was a difference, False if they were already in sync
        ret3 -- None if there were no errors/exceptions, 'Error string' otherwise
    """

    is_change = False
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        out, err = command.get_command_output(
            'diff "/etc/sysconfig/ctdb" "%s/lock/ctdb"' % str(config_dir), True, True)
        if err:
            shutil.copyfile('%s/lock/ctdb' %
                            str(config_dir), '/etc/sysconfig/ctdb')
            is_change = True

        out, err = command.get_command_output(
            'diff "/etc/ctdb/nodes" "%s/lock/nodes"' % str(config_dir), True, True)
        if err:
            shutil.copyfile('%s/lock/nodes' %
                            str(config_dir), '/etc/ctdb/nodes')
            is_change = True

        out, err = command.get_command_output(
            'diff "/etc/ctdb/public_addresses" "%s/lock/public_addresses"' % str(config_dir), True, True)
        if err:
            shutil.copyfile('%s/lock/public_addresses' %
                            str(config_dir), '/etc/ctdb/public_addresses')
            is_change = True

        lg, err = logger.get_script_logger(
            'Admin volume mounter: Sync CTDB config files', '/var/log/integralstor/scripts.log', level=logging.DEBUG)
        if is_change == True:
            logger.log_or_print(
                'ctdb related files were synced.', lg, level='info')
            logger.log_or_print(
                'Restarting ctdb.', lg, level='debug')
            out, err = command.get_command_output(
                'service ctdb restart', False, True)
            if not err and out:
                logger.log_or_print(
                    'Service ctdb: %s' % out, lg, level='debug')
            else:
                logger.log_or_print(
                    'Service ctdb error: %s' % err, lg, level='error')
        elif is_change == False:
            logger.log_or_print(
                'ctdb related files are in sync.', lg, level='info')

    except Exception, e:
        return False, is_change, "Couldn't sync ctdb files: %s" % str(e)
def mount_and_configure():
    lg = None
    try:
        lg, err = logger.get_script_logger(
            'Admin volume mounter', '/var/log/integralstor/scripts.log', level=logging.DEBUG)

        logger.log_or_print(
            'Admin volume mounter initiated.', lg, level='info')

        pog, err = grid_ops.is_part_of_grid()
        if err:
            raise Exception(err)

        # If the localhost is part of the gridcell, proceed
        if pog:
            logger.log_or_print('Checking glusterd service', lg, level='debug')
            service = 'glusterd'
            status, err = services_management.get_service_status([service])
            if err:
                raise Exception(err)
            logger.log_or_print('Service %s status is %s' % (
                service, status['status_code']), lg, level='debug')
            if status['status_code'] != 0:
                logger.log_or_print(
                    'Service %s not started so restarting' % service, lg, level='error')
                out, err = command.get_command_output(
                    'service %s restart' % service, False, True)
                if not err and out:
                    logger.log_or_print('Service %s: %s' %
                                        (service, out), lg, level='debug')
                else:
                    logger.log_or_print('Service %s error : %s' % (
                        service, err), lg, level='error')

            admin_vol_name, err = config.get_admin_vol_name()
            if err:
                raise Exception(err)

            # Get the config dir - the mount point.
            config_dir, err = config.get_config_dir()
            if err:
                raise Exception(err)

            ag, err = grid_ops.is_admin_gridcell()
            if err:
                raise Exception(err)

            admin_gridcells, err = grid_ops.get_admin_gridcells()
            if err:
                raise Exception(err)

            is_pooled = False
            peer_list, err = gluster_trusted_pools.get_peer_list()
            if peer_list:
                is_pooled = True

            is_mounted = False
            # mount only if the localhost is pooled
            if is_pooled:
                is_mounted, err = grid_ops.is_admin_vol_mounted_local()
                if not is_mounted:
                    str = 'Admin volume is not mounted. Will attempt to mount now.' 
                    logger.log_or_print(str, lg, level='error')

                    # Try to mount
                    (ret, rc), err = command.execute_with_rc(
                        'mount -t glusterfs localhost:/%s %s' % (admin_vol_name, config_dir))
                    if err:
                        str = 'Mount from localhost failed.' 
                        logger.log_or_print(str, lg, level='error')
                    elif (not err) and (rc == 0):
                        is_access, err = assert_admin_vol_mount()
                        if err:
                            raise Exception(err)

                        sync, is_change, error = sync_ctdb_files()
                        if error:
                            # It's only a best-effort, it will try next
                            # minute again.
                            pass
                        if sync == False:
                            #raise Exception (err)
                            pass

                        # Restart nginx
                        out, err = command.get_command_output(
                            'service nginx restart', False, True)
                        if not err and out:
                            logger.log_or_print(
                                'Service nginx: %s' % out, lg, level='debug')
                        else:
                            logger.log_or_print(
                                'Service nginx error : %s' % err, lg, level='error')
                        # Restart uwsgi
                        out, err = command.get_command_output(
                            'service uwsgi restart', False, True)
                        if not err and out:
                            logger.log_or_print(
                                'Service uwsgi: %s' % out, lg, level='debug')
                        else:
                            logger.log_or_print(
                                'Service uwsgi error : %s' % err, lg, level='error')

                        if ag:
                            # Restart salt-master
                            out, err = command.get_command_output(
                                'service salt-master restart', False, True)
                            if not err and out:
                                logger.log_or_print(
                                    'Service salt-master: %s' % out, lg, level='debug')
                            else:
                                logger.log_or_print(
                                    'Service salt-master error : %s' % err, lg, level='error')
                            # Restart salt-minion
                        out, err = command.get_command_output(
                            'service salt-minion restart', False, True)
                        if not err and out:
                            logger.log_or_print(
                                'Service salt-minion: %s' % out, lg, level='debug')
                        else:
                            logger.log_or_print(
                                'Service salt-minion error : %s' % err, lg, level='error')

                    str = 'Admin vol is mounted'
                    logger.log_or_print(str, lg, level='info')

                # Admin volume is mounted, perform required checks
                else:
                    sync, is_change, err = sync_ctdb_files()
                    if err:
                        raise Exception(err)
                    if sync == False:
                        raise Exception(err)

                    logger.log_or_print('Checking services', lg, level='debug')
                    service_list = ['nginx','ctdb','salt-minion']
                    if ag:
                        service_list.append('salt-master')

                    for service in service_list:
                        status, err = services_management.get_service_status([
                                                                             service])
                        if err:
                            raise Exception(err)
                        logger.log_or_print('Service %s status is %s' % (
                            service, status['status_code']), lg, level='debug')
                        if status['status_code'] != 0:
                            logger.log_or_print(
                                'Service %s is not active, restarting' % service, lg, level='error')
                            out, err = command.get_command_output(
                                'service %s restart' % service, False, True)
                            if not err and out:
                                logger.log_or_print('Service %s: %s' % (
                                    service, out), lg, level='debug')
                            else:
                                logger.log_or_print('Service %s error : %s' % (
                                    service, err), lg, level='error')

                    # UWSGI service config not complete so need to check
                    # against the actual process name
                    (ret, rc), err = command.execute_with_rc(
                        'pidof uwsgi', shell=True)
                    if rc != 0:
                        logger.log_or_print(
                            'Service uwsgi is not active, restarting', lg, level='error')
                        out, err = command.get_command_output(
                            'service uwsgi restart', False, True)
                        if not err and out:
                            logger.log_or_print(
                                'Service uwsgi: %s' % out, lg, level='debug')
                        else:
                            logger.log_or_print(
                                'Service uwsgi error : %s' % err, lg, level='error')

                    str = 'Admin volume is already mounted'
                    logger.log_or_print(str, lg, level='info')

    except Exception, e:
        st = 'Error mounting admin volume : %s' % e
        logger.log_or_print(st, lg, level='critical')
        return False, st
def generate_smb_conf():
    """Generate the complete of smb.conf that is specific to gridcell

    Returns True on success and False otherwise
    """
    try:
        d, err = cifs_utils.get_auth_settings()
        if err:
            raise Exception(err)

        smb_conf_path, err = config.get_smb_conf_path()
        if err:
            raise Exception(err)

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        # For customer specific or non configurable smb.conf lines
        extra_share_param_lines = None
        extra_global_param_lines = None
        if os.path.isfile('%s/customer_specific/extra_smb_share_params.conf' % config_dir):
            with open('%s/customer_specific/extra_smb_share_params.conf' % config_dir, 'r') as f1:
                extra_share_param_lines = f1.readlines()
        if os.path.isfile('%s/customer_specific/extra_smb_global_params.conf' % config_dir):
            with open('%s/customer_specific/extra_smb_global_params.conf' % config_dir, 'r') as f1:
                extra_global_param_lines = f1.readlines()
        # print extra_share_param_lines
        # print extra_global_param_lines

        with open("%s/smb.conf" % smb_conf_path, "w+") as f:
            ret, err = cifs_utils.generate_global_header(f)
            if err:
                raise Exception(err)

            ret, err = _generate_gridcell_specific_global_section(f, d)
            if err:
                raise Exception(err)

            ret, err = cifs_utils.generate_common_global_section(
                f, d, extra_global_param_lines)
            if err:
                raise Exception(err)

            ret, err = _generate_local_share(f)
            if err:
                raise Exception(err)

            shl, err = cifs_utils.get_shares_list()
            if err:
                raise Exception(err)

            if shl:
                for share in shl:
                    ul = []
                    gl = []
                    if not share["guest_ok"]:
                        vul, err = get_valid_users_list(
                            share["share_id"])
                        if err:
                            raise Exception(err)
                        # print 'vul is ', vul
                        if vul:
                            for vu in vul:
                                if vu["grp"]:
                                    gl.append(vu["name"])
                                else:
                                    ul.append(vu["name"])
                    share_dict = {}
                    share_dict['share_name'] = share['name']
                    share_dict['vol_name'] = share['vol']
                    share_dict['workgroup'] = d['workgroup']
                    share_dict['path'] = share['path']
                    share_dict['read_only'] = share['read_only']
                    share_dict['browseable'] = share['browseable']
                    share_dict['guest_ok'] = share['guest_ok']
                    share_dict['comment'] = share['comment']
                    share_dict['auth_method'] = d['security']
                    share_dict['user_list'] = ul
                    share_dict['group_list'] = gl
                    ret, err = _generate_share_section(
                        f, share_dict, extra_share_param_lines)
                    if err:
                        raise Exception(err)

        ret, errors = _reload_config()
        if errors:
            raise Exception(errors)

    except Exception, e:
        return False, 'Error generating CIFS configuration :%s' % str(e)
def mount_and_configure():
    lg = None
    try:
        lg, err = logger.get_script_logger('Admin volume mounter',
                                           '/var/log/integralstor/scripts.log',
                                           level=logging.DEBUG)

        logger.log_or_print('Admin volume mounter initiated.',
                            lg,
                            level='info')

        pog, err = grid_ops.is_part_of_grid()
        if err:
            raise Exception(err)

        # If the localhost is part of the gridcell, proceed
        if pog:
            logger.log_or_print('Checking glusterd service', lg, level='debug')
            service = 'glusterd'
            status, err = services_management.get_service_status([service])
            if err:
                raise Exception(err)
            logger.log_or_print('Service %s status is %s' %
                                (service, status['status_code']),
                                lg,
                                level='debug')
            if status['status_code'] != 0:
                logger.log_or_print('Service %s not started so restarting' %
                                    service,
                                    lg,
                                    level='error')
                out, err = command.get_command_output(
                    'service %s restart' % service, False, True)
                if not err and out:
                    logger.log_or_print('Service %s: %s' % (service, out),
                                        lg,
                                        level='debug')
                else:
                    logger.log_or_print('Service %s error : %s' %
                                        (service, err),
                                        lg,
                                        level='error')

            admin_vol_name, err = config.get_admin_vol_name()
            if err:
                raise Exception(err)

            # Get the config dir - the mount point.
            config_dir, err = config.get_config_dir()
            if err:
                raise Exception(err)

            ag, err = grid_ops.is_admin_gridcell()
            if err:
                raise Exception(err)

            admin_gridcells, err = grid_ops.get_admin_gridcells()
            if err:
                raise Exception(err)

            is_pooled = False
            peer_list, err = gluster_trusted_pools.get_peer_list()
            if peer_list:
                is_pooled = True

            is_mounted = False
            # mount only if the localhost is pooled
            if is_pooled:
                is_mounted, err = grid_ops.is_admin_vol_mounted_local()
                if not is_mounted:
                    str = 'Admin volume is not mounted. Will attempt to mount now.'
                    logger.log_or_print(str, lg, level='error')

                    # Try to mount
                    (ret, rc), err = command.execute_with_rc(
                        'mount -t glusterfs localhost:/%s %s' %
                        (admin_vol_name, config_dir))
                    if err:
                        str = 'Mount from localhost failed.'
                        logger.log_or_print(str, lg, level='error')
                    elif (not err) and (rc == 0):
                        is_access, err = assert_admin_vol_mount()
                        if err:
                            raise Exception(err)

                        sync, is_change, error = sync_ctdb_files()
                        if error:
                            # It's only a best-effort, it will try next
                            # minute again.
                            pass
                        if sync == False:
                            #raise Exception (err)
                            pass

                        # Restart nginx
                        out, err = command.get_command_output(
                            'service nginx restart', False, True)
                        if not err and out:
                            logger.log_or_print('Service nginx: %s' % out,
                                                lg,
                                                level='debug')
                        else:
                            logger.log_or_print('Service nginx error : %s' %
                                                err,
                                                lg,
                                                level='error')
                        # Restart uwsgi
                        out, err = command.get_command_output(
                            'service uwsgi restart', False, True)
                        if not err and out:
                            logger.log_or_print('Service uwsgi: %s' % out,
                                                lg,
                                                level='debug')
                        else:
                            logger.log_or_print('Service uwsgi error : %s' %
                                                err,
                                                lg,
                                                level='error')

                        if ag:
                            # Restart salt-master
                            out, err = command.get_command_output(
                                'service salt-master restart', False, True)
                            if not err and out:
                                logger.log_or_print('Service salt-master: %s' %
                                                    out,
                                                    lg,
                                                    level='debug')
                            else:
                                logger.log_or_print(
                                    'Service salt-master error : %s' % err,
                                    lg,
                                    level='error')
                            # Restart salt-minion
                        out, err = command.get_command_output(
                            'service salt-minion restart', False, True)
                        if not err and out:
                            logger.log_or_print('Service salt-minion: %s' %
                                                out,
                                                lg,
                                                level='debug')
                        else:
                            logger.log_or_print(
                                'Service salt-minion error : %s' % err,
                                lg,
                                level='error')

                    str = 'Admin vol is mounted'
                    logger.log_or_print(str, lg, level='info')

                # Admin volume is mounted, perform required checks
                else:
                    sync, is_change, err = sync_ctdb_files()
                    if err:
                        raise Exception(err)
                    if sync == False:
                        raise Exception(err)

                    logger.log_or_print('Checking services', lg, level='debug')
                    service_list = ['nginx', 'ctdb', 'salt-minion']
                    if ag:
                        service_list.append('salt-master')

                    for service in service_list:
                        status, err = services_management.get_service_status(
                            [service])
                        if err:
                            raise Exception(err)
                        logger.log_or_print('Service %s status is %s' %
                                            (service, status['status_code']),
                                            lg,
                                            level='debug')
                        if status['status_code'] != 0:
                            logger.log_or_print(
                                'Service %s is not active, restarting' %
                                service,
                                lg,
                                level='error')
                            out, err = command.get_command_output(
                                'service %s restart' % service, False, True)
                            if not err and out:
                                logger.log_or_print('Service %s: %s' %
                                                    (service, out),
                                                    lg,
                                                    level='debug')
                            else:
                                logger.log_or_print('Service %s error : %s' %
                                                    (service, err),
                                                    lg,
                                                    level='error')

                    # UWSGI service config not complete so need to check
                    # against the actual process name
                    (ret, rc), err = command.execute_with_rc('pidof uwsgi',
                                                             shell=True)
                    if rc != 0:
                        logger.log_or_print(
                            'Service uwsgi is not active, restarting',
                            lg,
                            level='error')
                        out, err = command.get_command_output(
                            'service uwsgi restart', False, True)
                        if not err and out:
                            logger.log_or_print('Service uwsgi: %s' % out,
                                                lg,
                                                level='debug')
                        else:
                            logger.log_or_print('Service uwsgi error : %s' %
                                                err,
                                                lg,
                                                level='error')

                    str = 'Admin volume is already mounted'
                    logger.log_or_print(str, lg, level='info')

    except Exception, e:
        st = 'Error mounting admin volume : %s' % e
        logger.log_or_print(st, lg, level='critical')
        return False, st
def edit_ntp_settings(request):

    return_dict = {}
    try:
        return_dict['base_template'] = "services_base.html"
        return_dict["page_title"] = 'Configure NTP settings'
        return_dict['tab'] = 'service_ntp_tab'
        return_dict["error"] = 'Error configuring NTP settings'

        admin_vol_mountpoint, err = config.get_config_dir()
        if err:
            raise Exception(err)
        if err:
            raise Exception(err)
        if request.method == "GET":
            ntp_servers, err = ntp.get_ntp_servers()
            if err:
                raise Exception(err)
            if not ntp_servers:
                form = common_forms.ConfigureNTPForm()
            else:
                form = common_forms.ConfigureNTPForm(
                    initial={'server_list': ','.join(ntp_servers)})
            url = "edit_ntp_settings.html"
        else:
            form = common_forms.ConfigureNTPForm(request.POST)
            if form.is_valid():
                #iv_logging.debug("Got valid request to change NTP settings")
                cd = form.cleaned_data
                si, err = system_info.load_system_config()
                if err:
                    raise Exception(err)
                if not si:
                    raise Exception(
                        'Error loading system configuration. System config missing?')
                server_list = cd["server_list"]
                if ',' in server_list:
                    slist = server_list.split(',')
                else:
                    slist = server_list.split(' ')
                primary_server = "gridcell-pri.integralstor.lan"
                secondary_server = "gridcell-sec.integralstor.lan"
                # First create the ntp.conf file for the primary and secondary
                # nodes
                temp = tempfile.NamedTemporaryFile(mode="w")
                temp.write("driftfile /var/lib/ntp/drift\n")
                temp.write(
                    "restrict default kod nomodify notrap nopeer noquery\n")
                temp.write(
                    "restrict -6 default kod nomodify notrap nopeer noquery\n")
                temp.write("includefile /etc/ntp/crypto/pw\n")
                temp.write("keys /etc/ntp/keys\n")
                temp.write("\n")
                for server in slist:
                    temp.write("server %s iburst\n" % server)
                temp.flush()
                shutil.move(temp.name, "%s/ntp/primary_ntp.conf" %
                            admin_vol_mountpoint)
                temp1 = tempfile.NamedTemporaryFile(mode="w")
                temp1.write("server %s iburst\n" % primary_server)
                temp1.write("server %s iburst\n" % secondary_server)
                for s in si.keys():
                    temp1.write("peer %s iburst\n" % s)
                temp1.write("server 127.127.1.0\n")
                temp1.write("fudge 127.127.1.0 stratum 10\n")
                temp1.flush()
                shutil.move(temp1.name, "%s/ntp/secondary_ntp.conf" %
                            admin_vol_mountpoint)
                return django.http.HttpResponseRedirect("/view_ntp_settings?saved=1")
            else:
                # invalid form
                #iv_logging.debug("Got invalid request to change NTP settings")
                url = "edit_ntp_settings.html"
        return_dict["form"] = form
        return django.shortcuts.render_to_response(url, return_dict, context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        s = str(e)
        if "Another transaction is in progress".lower() in s.lower():
            return_dict["error_details"] = "An underlying storage operation has locked a volume so we are unable to process this request. Please try after a couple of seconds"
        else:
            return_dict["error_details"] = "An error occurred when processing your request : %s" % s
        return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
Exemple #38
0
def establish_default_configuration(client, si, admin_gridcells):
    try:

        platform_root, err = config.get_platform_root()
        if err:
            raise Exception(err)

        defaults_dir, err = config.get_defaults_dir()
        if err:
            raise Exception(err)
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        ss_path, err = config.get_system_status_path()
        if err:
            raise Exception(err)
        tmp_path, err = config.get_tmp_path()
        if err:
            raise Exception(err)

        print "\nCopying the default configuration onto the IntegralStor administration volume."
        shutil.copytree("%s/db" % defaults_dir, "%s/db" % config_dir)

        # Create integral view DB
        ret, err = command.get_command_output('rm -rf %s/db/integral_view_config.db' % config_dir, shell=True)
        if err:
            pass
        ret, err = command.get_command_output('sqlite3 %s/db/integral_view_config.db < %s/db/integral_view_config.schema' % (config_dir,config_dir), shell=True)
        if err:
            raise Exception('Could not create DB: %s' % err)

        shutil.copytree("%s/ntp" % defaults_dir, "%s/ntp" % config_dir)
        shutil.copytree("%s/logs" % defaults_dir, "%s/logs" % config_dir)

        # Delete any existing NTP file
        r2 = client.cmd('roles:master', 'cmd.run_all', [
                        'rm /etc/ntp.conf'], expr_form='grain')

        # Link the new NTP conf file on the primary onto the admin vol
        ip_info, err = networking.get_ip_info('bond0')
        if err:
            raise Exception(err)
        ip_l = struct.unpack('!L', socket.inet_aton(ip_info['ipaddr']))[0]
        netmask_l = struct.unpack(
            '!L', socket.inet_aton(ip_info['netmask']))[0]
        network_l = ip_l & netmask_l
        network = socket.inet_ntoa(struct.pack('!L', network_l))
        r2 = client.cmd('roles:master', 'integralstor.configure_ntp_master', admin_gridcells, kwarg={
                        'network': network, 'netmask': ip_info['netmask']}, expr_form='grain')
        if r2:
            errors = ''
            for node, ret in r2.items():
                if not ret[0]:
                    print r2
                    errors += "Error configuring NTP on %s : %s" % (
                        node, ret[1])
            if errors:
                raise Exception(errors)

        # Create a home for the manifest and status files and move the
        # previously generated files here..
        os.mkdir("%s/status" % config_dir)
        shutil.move("%s/master.manifest" % tmp_path, ss_path)
        shutil.move("%s/master.status" % tmp_path, ss_path)

        print "Copying the default configuration onto the IntegralStor administration volume... Done."
        print

        print "Setting up CIFS access.."

        os.mkdir("%s/lock" % config_dir)
        os.mkdir("%s/samba" % config_dir)

        os.mkdir("%s/logs/task_logs" % config_dir, 0777)

        print "Creating assert_admin_vol_mount file"
        with open('%s/lock/assert_admin_vol_mount' % config_dir, 'w') as f:
            f.close()
        print "Creating assert_admin_vol_mount file... Done"
        print

        print "Creating CTDB config file"
        rc, errors = ctdb.create_config_file()
        if not rc:
            if errors:
                raise Exception(errors)
            else:
                raise Exception(
                    "Error creating the CTDB configuration file : Reason unknown")
        print "Creating CTDB config file... Done"
        print

        print "Creating CTDB nodes file"

        ip_list = []
        for node_name, node_info in si.items():
            if "interfaces" in node_info and "bond0" in node_info["interfaces"] and "inet" in node_info["interfaces"]["bond0"] and len(node_info["interfaces"]["bond0"]["inet"]) == 1:
                ip_list.append(
                    "%s" % node_info["interfaces"]["bond0"]["inet"][0]["address"])

        rc, errors = ctdb.add_to_nodes_file(ip_list)
        if not rc:
            if errors:
                raise Exception(errors)
            else:
                raise Exception(
                    "Error creating the CTDB nodes file : Reason unknown")
        print "Creating CTDB nodes file... Done"
        print

        # Create an empty public_addresses file. Entries to the file will be
        # added later from the menus.
        print "Creating CTDB public_addresses file(blank)"
        rc, err = ctdb.mod_public_addresses(None, action='create')
        if err:
            raise Exception(err)
        print "Creating CTDB public_addresses file(blank)... Done"

        print "Placing CTDB files"
        shutil.copyfile('/etc/sysconfig/ctdb', '%s/lock/ctdb' % config_dir)
        r2 = client.cmd('*', 'cmd.run_all', ['rm /etc/sysconfig/ctdb'])
        r2 = client.cmd('*', 'cmd.run_all',
                        ['cp %s/lock/ctdb /etc/sysconfig/ctdb' % config_dir])
        if r2:
            for node, ret in r2.items():
                if ret["retcode"] != 0:
                    print r2
                    errors = "Error placing the CTDB config file on %s" % node
                    raise Exception(errors)
        print "Placing CTDB files... Done"
        print

        # The initial add_to_nodes_file created the initial nodes file in admin
        # vol. So pull it to corresponding /etc/ctdb/ path on all nodes

        print "Placing CTDB nodes files"
        # next line is redundant?
        shutil.copyfile('%s/lock/nodes' % config_dir, '/etc/ctdb/nodes')
        r2 = client.cmd('*', 'cmd.run_all', ['rm -f /etc/ctdb/nodes'])
        r2 = client.cmd('*', 'cmd.run_all',
                        ['cp %s/lock/nodes /etc/ctdb/nodes' % config_dir])
        if r2:
            for node, ret in r2.items():
                if ret["retcode"] != 0:
                    print r2
                    errors = "Error linking to the CTDB nodes file on %s" % node
                    raise Exception(errors)
        print "Placing CTDB nodes files... Done"
        print

        print "Placing CTDB public_addresses files"
        # next line is redundant?
        shutil.copyfile('%s/lock/public_addresses' %
                        config_dir, '/etc/ctdb/public_addresses')
        r2 = client.cmd('*', 'cmd.run_all',
                        ['rm -f /etc/ctdb/public_addresses'])
        r2 = client.cmd(
            '*', 'cmd.run_all', ['cp %s/lock/public_addresses /etc/ctdb/public_addresses' % config_dir])
        if r2:
            for node, ret in r2.items():
                if ret["retcode"] != 0:
                    print r2
                    errors = "Error linking to the CTDB nodes file on %s" % node
                    raise Exception(errors)
        print "Placing CTDB public_addresses files... Done"
        print

        print "Linking smb.conf files"
        shutil.copyfile('%s/samba/smb.conf' % defaults_dir,
                        '%s/lock/smb.conf' % config_dir)
        r2 = client.cmd('*', 'cmd.run_all', ['rm -f /etc/samba/smb.conf'])
        r2 = client.cmd(
            '*', 'cmd.run_all', ['ln -s %s/lock/smb.conf /etc/samba/smb.conf' % config_dir])
        if r2:
            for node, ret in r2.items():
                if ret["retcode"] != 0:
                    print r2
                    errors = "Error linking to the smb.conf file on %s" % node
                    raise Exception(errors)
        print "Linking smb.conf files... Done"
        print

        print "Linking NFS exports file"
        shutil.copyfile('%s/nfs/exports' % defaults_dir,
                        '%s/lock/exports' % config_dir)
        r2 = client.cmd('*', 'cmd.run_all', ['rm -f /etc/exports'])
        r2 = client.cmd(
            '*', 'cmd.run_all', ['ln -s %s/lock/exports /etc/exports' % config_dir])
        if r2:
            for node, ret in r2.items():
                if ret["retcode"] != 0:
                    print r2
                    errors = "Error linking to the NFS exports file on %s" % node
                    raise Exception(errors)
        print "Linking NFS exports file... Done"
        print

        print "Linking Kerberos config file"
        r2 = client.cmd('*', 'cmd.run_all', ['rm /etc/krb5.conf'])
        with open('%s/lock/krb5.conf' % config_dir, 'w') as f:
            f.close()
        r2 = client.cmd('*', 'cmd.run_all',
                        ['ln -s %s/lock/krb5.conf /etc/krb5.conf' % config_dir])
        if r2:
            for node, ret in r2.items():
                if ret["retcode"] != 0:
                    print r2
                    errors = "Error linking to the krb5.conf file on %s" % node
                    raise Exception(errors)
        print "Linking Kerberos config file... Done"
        print

        print "Setting appropriate boot time init files."
        r2 = client.cmd('*', 'cmd.run_all', ['rm /etc/rc.local'])
        r2 = client.cmd('*', 'cmd.run_all', ['rm /etc/rc.d/rc.local'])
        r2 = client.cmd(
            '*', 'cmd.run_all', ['cp %s/rc_local/rc.local /etc/rc.local' % defaults_dir])
        r2 = client.cmd('*', 'cmd.run_all', ['chmod 755 /etc/rc.local'])
        if r2:
            for node, ret in r2.items():
                if ret["retcode"] != 0:
                    print r2
                    errors = "Error setting the appropriate rc.local file on %s" % node
                    raise Exception(errors)
        print "Setting appropriate boot time init files... Done"
        print

    except Exception, e:
        return False, 'Error establishing default configuration : %s' % str(e)
Exemple #39
0
def download_system_configuration(request):
    """ Download the complete configuration stored in get_config_dir()"""

    return_dict = {}
    try:
        return_dict['base_template'] = "system_base.html"
        return_dict["page_title"] = 'Download system configuration'
        return_dict['tab'] = 'download_config_tab'
        return_dict["error"] = 'Error downloading system configuration'

        if request.method == 'POST':
            config_dir, err = config.get_config_dir()
            if err:
                raise Exception(err)
            # Remove trailing '/'
            if config_dir[len(config_dir) - 1] == '/':
                config_dir = config_dir[:len(config_dir) - 1]

            display_name = 'integralstor_config'
            zf_name = '/tmp/integralstor_config.zip'
            zf = zipfile.ZipFile(zf_name, 'w')
            top_component = config_dir[config_dir.rfind('/') + 1:]
            for dirname, subdirs, files in os.walk(config_dir):
                for filename in files:
                    # print os.path.join(dirname, filename)
                    absname = os.path.abspath(os.path.join(dirname, filename))
                    arcname = '%s/%s' % (top_component,
                                         absname[len(config_dir) + 1:])
                    # print arcname
                    zf.write(absname, arcname)
            zf.close()

            response = django.http.HttpResponse()
            response['Content-disposition'] = 'attachment; filename=%s.zip' % (
                display_name)
            response['Content-type'] = 'application/x-compressed'
            with open(zf_name, 'rb') as f:
                byte = f.read(1)
                while byte:
                    response.write(byte)
                    byte = f.read(1)
            response.flush()

            return response

        # either a get or an invalid form so send back form
        return django.shortcuts.render_to_response(
            'download_system_configuration.html',
            return_dict,
            context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        s = str(e)
        if "Another transaction is in progress".lower() in s.lower():
            return_dict[
                "error"] = "An underlying storage operation has locked a volume so we are unable to process this request. Please try after a couple of seconds"
        else:
            return_dict[
                "error"] = "An error occurred when processing your request : %s" % s
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))