ルーターのコンフィグを git にぶち込んで世代管理してみたはなし

この記事を三行で

  • みんな好き勝手な名前でコンフィグファイルをアップロードしてサーバーがパンク&どれが最新版かわからない状況に
  • 命名規則を作り、サーバーに置いておくコンフィグを最新世代1つのみとした
  • さらにファイルを社内 GitLab に自動アップロードし履歴確認ができるようにした

 GitLab 使git 使


IPTFTP5HDD調...
$ df -h
Filesystem          サイズ  使用  残り 使用% マウント位置
/dev/cciss/c0d0p3     195G  195G     0 100% /
/dev/cciss/c0d0p1      99M   13M   81M  14% /boot

Σ(ω) 
$ ll /var/tftp/ | wc -l
239662

Σ(ω) 24

1
$ ll /var/tftp | grep tkycore01
-rw-rw-rw- 1 nobody nobody  124862  10月  10  2015  20140303_tkycore01-confg
-rw-rw-rw- 1 nobody nobody  127522  10月  10  2015  after_tkycore01.config
-rw-rw-rw- 1 nobody nobody  123456  10月  10  2015  before_tkycore01.config
-rw-rw-rw- 1 nobody nobody  122899  10月  10  2015  tkycore01
-rw-rw-rw- 1 nobody nobody  123456  10月  10  2015  tkycore01-confg
-rw-rw-rw- 1 nobody nobody  126984  10月  10  2015  tkycore01-confg.txt
-rw-rw-rw- 1 nobody nobody  119853  10月  10  2015  tkycore01-confg-old
-rw-rw-rw- 1 nobody nobody  127556  10月  10  2015  tkycore01-confg_20130712.config
-rw-rw-rw- 1 nobody nobody  126574  10月  10  2015  tkycore01-config



 RANCID 


(一)TFTP Cisco  -confg 

(二)TFTP GitLab 




Before

f:id:miyahan:20160713225607p:plain

 : TFTP  !!

 :   


After

f:id:miyahan:20160714202657p:plain

-confg 

-confg 




TFTP調

 Telnetter 使 Telnet  copy running-config tftp: TFTP14
$ ll /var/tftp | grep tkycore01
-rw-rw-rw- 1 nobody nobody     124862   5月  30  00:00 tkycore01-confg

(´ω) 

1

git 使


git 使

使() git 使(GitLab) push  GitLab 使

 PHP  PHP  Python 使



f:id:miyahan:20160531061945p:plain

(一)TFTP rsync 

(二)

(三) git add / git rm GitLab push

1. rsync 

os.chdir(os.path.dirname(os.path.abspath(__file__))+'/')

args = [
    'sshpass', '-p', rsync_remote_pass, 'rsync', '-auvz', '--delete', 
    '--include-from', 'rsync_include.list',
    '--exclude', "*", rsync_remote_uri, 'tftp/'
]
subprocess.call(args)

python  rsync 


sshpass 使

expect \"password:\"  sshpassssh/scp - Qiita

SSHd



--include-from 使

--delete 使

2. 


 git /
# 既存ファイル一覧を取得 (削除対象フラグとして使う)
arealist = [
    'tokyo', 'kanagawa', 'chiba', 'saitama', 'ibaraki',
    'tochigi', 'gunma', 'yamanashi', 'nagano', 'nigata',
    'miyagi', 'fukushima', 'iwate', 'aomori', 'yamagata',
    'akita', 'hokkaido'
]

old_files = {}
for area in arealist:
    for dirpath, dirnames, filenames in os.walk(area):
        for file in filenames:
            old_files[file] = dirpath+'/'+file

()
# TFTPディレクトリから一覧を取得、ファイル名解析しビルごとのディレクトリへコピー
pattern = r"^(secret)$"
host_re = re.compile(pattern)

for file in os.listdir('tftp'):
    # ファイル名解析
    match = host_re.search(file)
    
    if match is None:
        print('\033[91m**WARNING**\033[0m Invalid filename format : '+file)
        continue
    
    bldg  = match.group(2)
    area  = areacode[match.group(3)]
    src = 'tftp/'+file
    dst = area+'/'+bldg+'/'
    
    # 削除フラグを消す
    if file in old_files: old_files.pop(file)
    
    # すでに同じファイルがある場合はスキップ
    if os.path.isfile(dst+'/'+file) and filecmp.cmp(src, dst+'/'+file): continue
    
    # コピー先ディレクトリが無ければ作成
    if not os.path.isdir(dst): os.makedirs(dst)
    
    # ファイルをビルディレクトリへコピー
    shutil.copy2(src, dst)
    args = ['git', 'add', dst+'/'+file]
    subprocess.call(args)

 rsync 
# TFTPディレクトリから無くなったファイルを削除
for path in old_files.values():
    args = ['git', 'rm', path]
    subprocess.call(args)
    print('Removed deleted file: '+path)

TFTPgit rm 

3. git push

d = datetime.datetime.today()
today = '%s/%s/%s' % (d.year, d.month, d.day)

args = ['git', 'commit', '-m', 'auto backup at '+today]
subprocess.call(args)

args = ['git', 'push', '-u', 'origin', 'master']
subprocess.call(args)

python  git  GitLab  push 

11 GitLab  push 

git 使


Web

f:id:miyahan:20160531014639p:plainf:id:miyahan:20160531014648p:plain
git 使


 : git log <> f:id:miyahan:20160605185659p:plain
 : git diff <ID> <ID> <> f:id:miyahan:20160605185654p:plain
調 : git log --since=<> --until=<> f:id:miyahan:20160605185658p:plain

 git 







 git 使





 | Python 





PHP 

 {}  ; $ PHP 

Python ()PHP使Python 使

PHP 

subprocess.call PHPOS

 PHP  foo.bar  PHP  foo+bar 








´-`


(Mattermost)

コンフィグ置き場を定期的にチェックして、異常を見つけたらチャットに通知するようにした。チャットからもHubot経由で実行できる。まるでIT企業😍 pic.twitter.com/v5ArlvdJsW

 Miyahan (@miyahancom) 2017120




使

 

  • 2016/6/10 : 新規ファイルがあるとファイル比較時にエラーで止まるバグがあったのでコード修正
    • -if filecmp.cmp(src, dst+'/'+file):
    • +if os.path.isfile(dst+'/'+file) and filecmp.cmp(src, dst+'/'+file):