2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

設定をコピーするツールをつくった

Last updated at Posted at 2016-03-29

個人的備忘録です。
環境ごとに同じ設定なんだけど将来変えるかもしれないとかで、
どう考えても絶対にまちがえる自信があったので間違いようがないように
指定した引数をコピー元としてそれと同じ設定のグループにだけコピーしてくれるツールを作りました。
人より忘れんぼうとうっかりなのでその防止ということです。
bashだとなんか配列の扱いほかだいぶ色々面倒な気がしたのでrubyでがんばってみたっす。

## バージョン定義
version = "v1"

## 同じ設定の環境グループを定義する
fuga_envs1 = ["fuga1","fuga3","fuga7","perf","fuga-test1","fuga-mng","fuga-test3"]
hoge_envs1 = ["hoge-dev1","hoge-dev6","hoge-prod","hoge-mng"]
piyo_envs1 = ["piyo-mng","piyo-dev1","piyo-dev6","piyo-prod","piyo6"]
nyan_envs1 = ["nyan-dev1","nyan-dev6","nyan-mng","nyan-perf","nyan-prod"]

### 同じ設定のグループ名を登録(これあんまいみなかった)
# same_setting_groups = %w("fuga_envs1" "hoge_envs1" "piyo_envs1" "nyan_envs1")
# puts same_setting_groups

## 引数からコピー元の環境を定義
sorce_file_env = ARGV[0]
# puts sorce_file_env

## コピー元環境が含まれる同じ設定のグループ名を特定
source_include_group = []
if fuga_envs1.include?("#{sorce_file_env}") 
 source_include_group=fuga_envs1
 region="us-xxxx-2"
elsif hoge_envs1.include?("#{sorce_file_env}")
 source_include_group=hoge_envs1
 region="us-xxxx-2"
elsif piyo_envs1.include?("#{sorce_file_env}")
 source_include_group=piyo_envs1 
 region="us-xxxx-2"
elsif nyan_envs1.include?("#{sorce_file_env}")
 source_include_group=nyan_envs1 
 region="ap-northxxxx-1"
end

## ディレクトリを定義(コピー元と同一である前提)
# dir=`/bin/find ./ -name "#{sorce_file_env}" -type d|/bin/sed -e "s/#{sorce_file_env}//g"`
pwd = Dir::pwd
pdir = "#{region}/#{version}"
# puts pdir

## コピーを実行
src = "#{pwd}/#{pdir}/#{sorce_file_env}/forward.conf"
dest_path = "#{pwd}/#{pdir}"
puts "source_file: #{src}"
# puts dest_path

#  system("\cp -pf #{pwd}/#{pdir}/#{sorce_file_env}/* #{pwd}/#{pdir}#{env}/")
require "fileutils"
source_include_group.each do |env|
 next if env.to_s == sorce_file_env.to_s
 dest = "#{dest_path}/#{env}/"
 puts "destination_dir: #{dest}"
 FileUtils.cp(src, dest)
end

バックアップとかはどうせgitで管理してるファイル群なので気にしていません。git checkoutすれば戻るし。
nextとto_sほかやりたいことは検索すると大体わかるいい時代ダナー。
これをつかえば絶対に間違えない気がします。定義があってれば。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?