建立局域网内使用的CentOS7-OpenStack源

by 无若

1. 先建立局域网内使用的CentOS7源

这个参看 http://www.cnblogs.com/gleaners/p/5735472.html

 

2. 抓取所有OpenStack的包,文件夹完全对应。
这里专门写了一个snatch_repo.py
代码如下:

#coding=utf-8
import requests
import re
import string
import urllib2 as url
import urllib
import Queue
import os
import sys
from contextlib import closing 

class ProgressBar(object):  
    def __init__(self, title, count=0.0, run_status=None, fin_status=None, total=100.0,    unit='', sep='/', chunk_size=1.0):  
        super(ProgressBar, self).__init__()  
        self.info = '[%s] %s %.2f %s %s %.2f %s'  
        self.title = title  
        self.total = total  
        self.count = count  
        self.chunk_size = chunk_size  
        self.status = run_status or ''  
        self.fin_status = fin_status or ' ' * len(self.statue)  
        self.unit = unit  
        self.seq = sep  
  
    def __get_info(self):  
        # [名称]状态 进度 单位 分割线 总数 单位  
        _info = self.info % (self.title, self.status, self.count/self.chunk_size, self.unit, self.seq, self.total/self.chunk_size, self.unit)  
        return _info  
  
    def refresh(self, count=1, status=None):  
        self.count += count  
        # if status is not None:  
        self.status = status or self.status  
        end_str = '\r'  
        if self.count >= self.total:  
            end_str = '\n'  
            self.status = status or self.fin_status  
              
        sys.stdout.flush()
        sys.stdout.write(self.__get_info()+end_str)  
        sys.stdout.flush()

def get_regex_outcome_list(text, regex):
    """
    获取文本的正则表达式截取
    """
    lst = []
    reobj = re.compile(regex)
    for match in reobj.finditer(text):
        #match.start()
        #match.end()
        lst.append(match.group())
    return lst

def wget_file(url, file_name):
    """
    下载文件
    """
    #r = requests.get(url)
    #with open(file_name, 'wb') as code:
    #    code.write(r.content)
    
    with closing(requests.get(url, stream=True)) as response:  
        chunk_size = 1024  
        content_size = float(response.headers['content-length'])  
        ''' 
        需要根据 response.status_code 的不同添加不同的异常处理 
        '''  
        print('content_size', content_size,response.status_code ,  )  
        progress = ProgressBar('processing:'  
                    , total=content_size  
                    , unit='KB'  
                    , chunk_size=chunk_size  
                    , run_status='download...'  
                    , fin_status='download ok!')  
            # chunk_size = chunk_size < content_size and chunk_size or content_size  
        with open(file_name, 'wb') as file:  
                for data in response.iter_content(chunk_size=chunk_size):  
                    file.write(data)  
                    progress.refresh(count=len(data))  
                      
        print('download ok!', ) 

def page_to_url(url, html):
    """
    解析页面上的html,提取href中的url
    """
    lst = get_regex_outcome_list(html, 'href=".*"')
    context = ''
    path_lst = []
    for item in lst:
        item = item.replace('"','')
        item = item.replace('href=', '')
        path_lst.append((url+item, item))
    return path_lst
 
def parser_path_list(init_url, path_lst, deep_queue):
    """
    解析路径列表,对路径做对应处理
    """
    for item in path_lst:
        url = item[0]
        file_name = item[1]
        if file_name[-1] == '/' and file_name != '../':
            deep_queue.put(url)
            dir_name = url[len(init_url):-1]
            try:
                os.mkdir(dir_name)
            except Exception, e:
                print e
        else:
            #pass
            file_path = url[len(init_url):-1]
            print file_path
            try:
                wget_file(url, file_path)
            except Exception, e:
                print e                
 
def visit_all_path(init_url):
    """
    遍历所有路径
    """
    r = requests.get(init_url)
    path_lst = page_to_url(init_url, r.text)
    deep_queue = Queue.Queue()
    parser_path_list(init_url, path_lst, deep_queue)

    while not deep_queue.empty():
        print deep_queue.qsize()
        url = deep_queue.get()
        r = requests.get(url)
        path_lst = page_to_url(url, r.text)
        parser_path_list(init_url, path_lst, deep_queue)
        
        
    return path_lst
    
    

if __name__ == '__main__':
    init_url = 'http://mirrors.aliyun.com/centos/7.2.1511/cloud/x86_64/openstack-liberty/'
    visit_all_path(init_url)

# 代码里面使用的是阿里云的openstack-liberty。

# 在做源的服务器(192.168.29.94)上建立对应的目录

mkdir -p /var/ftp/pub/cloud/openstack-liberty

# 将此代码(snatch_repo.py)拷贝至/var/ftp/pub/cloud/openstack-liberty/目录下

cp snatch_repo.py /var/ftp/pub/cloud/openstack-liberty/

# 由于snatch_repo.py中使用了requests模块,所以需要先安装requests模块

pip install requests

# 执行此脚本,下载,静静等待所有下载完毕

python snatch_repo.py

 

3. 清除老的repodata,生成新的repodata

cp /var/ftp/pub/cloud/openstack-liberty/repodata/repomd.xml /tmp/

yum -y install createrepo

cd /var/ftp/pub/cloud/openstack-liberty/

createrepo -g /tmp/repomd.xml .

 

4. 配置客户机(假设:192.168.29.80)的源设置

cd /etc/yum.repos.d/
nano loacl_network_openstack_liberty.repo

# 编辑如下内容

[loacl_network_openstack_liberty]
name=ftpyum
baseurl=ftp://192.168.29.94/pub/cloud/openstack-liberty
enabled=1
gpgcheck=1
gpgkey=ftp://192.168.29.94/pub/centos7/RPM-GPG-KEY-CentOS-7

# 重新设定repo

yum clean all
yum makecache

 

5. 客户机(假设:192.168.29.80)验证

yum install openstack-keystone

 

posted @ 2016-08-04 10:38  拾穗云谷  阅读(1960)  评论(1编辑  收藏  举报