OSDN Git Service

adds the APT Cacher NG autopilot feature. apt_utils-0.3.0
authorwhitestar <whitestar@users.osdn.me>
Sun, 23 Sep 2018 04:57:35 +0000 (13:57 +0900)
committerwhitestar <whitestar@users.osdn.me>
Sun, 23 Sep 2018 04:57:35 +0000 (13:57 +0900)
16 files changed:
cookbooks/apt_utils/CHANGELOG.md
cookbooks/apt_utils/Gemfile.lock [new file with mode: 0644]
cookbooks/apt_utils/README.md
cookbooks/apt_utils/Rakefile
cookbooks/apt_utils/attributes/default.rb
cookbooks/apt_utils/concourse.yml
cookbooks/apt_utils/fly-vars.local.yml [new file with mode: 0644]
cookbooks/apt_utils/fly-vars.yml
cookbooks/apt_utils/metadata.rb
cookbooks/apt_utils/recipes/apt-cacher-ng.rb
cookbooks/apt_utils/recipes/aptly-docker-compose.rb
cookbooks/apt_utils/spec/recipes/apt-cacher-ng_spec.rb
cookbooks/apt_utils/spec/recipes/source_spec.rb
cookbooks/apt_utils/templates/default/etc/cron.d/apt-cacher-ng-local [new file with mode: 0644]
cookbooks/apt_utils/templates/default/usr/local/bin/restart_acng-local [new file with mode: 0644]
cookbooks/apt_utils/version

index e69a1f4..18b57b3 100644 (file)
@@ -1,6 +1,11 @@
 apt_utils CHANGELOG
 ===================
 
+0.3.0
+-----
+- adds the APT Cacher NG autopilot feature.
+- refactoring.
+
 0.2.6
 -----
 - improves server key pair deployment for a aptly service.
diff --git a/cookbooks/apt_utils/Gemfile.lock b/cookbooks/apt_utils/Gemfile.lock
new file mode 100644 (file)
index 0000000..6fb9a3b
--- /dev/null
@@ -0,0 +1,22 @@
+GEM
+  remote: https://rubygems.org/
+  specs:
+    chef-api (0.8.0)
+      logify (~> 0.1)
+      mime-types
+    logify (0.2.0)
+    mime-types (3.2.2)
+      mime-types-data (~> 3.2015)
+    mime-types-data (3.2018.0812)
+    stove (6.0.0)
+      chef-api (~> 0.5)
+      logify (~> 0.2)
+
+PLATFORMS
+  ruby
+
+DEPENDENCIES
+  stove
+
+BUNDLED WITH
+   1.16.0
index 78a3e40..56c0513 100644 (file)
@@ -51,6 +51,10 @@ This cookbook includes apt utility (apt-cacher-ng, apt mirroring site setup and
 |`['apt_utils']['aptly']['docker-compose']['etc_dir']`|String||`"#{node['apt_utils']['aptly']['docker-compose']['app_dir']}/etc"`|
 |`['apt_utils']['aptly']['docker-compose']['data_dir']`|String|Persistent volume path or nil (unset).|`"#{node['apt_utils']['aptly']['docker-compose']['app_dir']}/data"`|
 |`['apt_utils']['aptly']['docker-compose']['config']`|Hash|`docker-compose.yml` configurations.|See `attributes/default.rb`|
+|`['apt_utils']['apt-cacher-ng']['autopilot']['enabled']`|Boolean|APT Cacher NG autopilot feature flag.|`false`|
+|`['apt_utils']['apt-cacher-ng']['autopilot']['log_dir']`|String|log file's directory path.|`'/var/log'`|
+|`['apt_utils']['apt-cacher-ng']['autopilot']['wait_time']`|String|wait time after booted.|`'120s'`|
+|`['apt_utils']['apt-cacher-ng']['autopilot']['check_period']`|String|service check period. `'#'` means inactive.|`'*/5 * * * *'`|
 |`['apt_utils']['apt-cacher-ng']['CacheDir']`|String|path to cache directory|`'/var/cache/apt-cacher-ng'`|
 |`['apt_utils']['apt-cacher-ng']['LogDir']`|String|path to log directory.|`'/var/log/apt-cacher-ng'`|
 |`['apt_utils']['apt-cacher-ng']['Port']`|String|service port number.|`'3142'`|
@@ -100,6 +104,7 @@ name 'aptly-docker-compose'
 description 'aptly by docker-compose'
 
 run_list(
+  # see https://osdn.net/projects/metasearch/scm/git/grid-chef-repo/blobs/master/roles/docker-new-repo.rb
   'role[docker]',
   'recipe[apt_utils::aptly-docker-compose]',
 )
index 8b85e6a..f1c82d7 100644 (file)
@@ -3,12 +3,56 @@ require 'rubocop/rake_task'
 require 'foodcritic'
 require 'stove/rake_task'
 
+tpl_cookbook = '00cookbook'
+cookbook_name = File.basename(Dir.pwd)
+
+desc 'Initialize project'
+task :init do
+  next if cookbook_name == tpl_cookbook
+
+  [
+    '.foodcritic',
+    '.rubocop.yml',
+    'Berksfile',
+    'concourse.yml',
+    'fly-vars.yml',
+    'fly-vars.local.yml',
+    'Gemfile',
+    'Gemfile.lock',
+    'version',
+  ].each {|conf|
+    sh "cp ../#{tpl_cookbook}/#{conf} ./" unless File.exist?(conf)
+  }
+
+  ruby [
+    %(-pne '$_.gsub!(/^cookbook-name: .*$/, "cookbook-name: #{cookbook_name}")'),
+    '-i fly-vars.local.yml',
+  ].join(' ')
+end
+
+desc 'Update project'
+task :update do
+  next if cookbook_name == tpl_cookbook
+
+  [
+    'Rakefile',
+    'concourse.yml',
+    'fly-vars.yml',
+    'Gemfile',
+    'Gemfile.lock',
+  ].each {|conf|
+    sh "cp ../#{tpl_cookbook}/#{conf} ./"
+  }
+end
+
+desc 'fly set-pipeline'
 task :'set-pipeline' do
   sh [
-    "fly -t $CC_TARGET sp -p #{File.basename(Dir.pwd)}-cookbook -c concourse.yml",
-    '-l fly-vars.yml -l ~/sec/credentials-prod.yml',
+    "fly -t $CC_TARGET sp -p #{cookbook_name}-cookbook -c concourse.yml",
+    '-l fly-vars.yml -l fly-vars.local.yml -l ~/sec/credentials-prod.yml',
   ].join(' ')
 end
+desc 'rake set-pipeline alias'
 task sp: 'set-pipeline'
 
 namespace :style do
index 099fe15..4bfe6be 100644 (file)
@@ -2,7 +2,7 @@
 # Cookbook Name:: apt_utils
 # Attributes:: default
 #
-# Copyright 2013-2017, whitestar
+# Copyright 2013-2018, whitestar
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -122,6 +122,12 @@ version_2_config = {
 }
 default['apt_utils']['aptly']['docker-compose']['config'] = version_2_config
 
+default['apt_utils']['apt-cacher-ng']['autopilot'] = {
+  'enabled' => false,
+  'log_dir' => '/var/log',
+  'wait_time' => '120s',
+  'check_period' => '*/5 * * * *',  # '#': inactive
+}
 default['apt_utils']['apt-cacher-ng']['CacheDir'] = '/var/cache/apt-cacher-ng'
 default['apt_utils']['apt-cacher-ng']['LogDir'] = '/var/log/apt-cacher-ng'
 default['apt_utils']['apt-cacher-ng']['SupportDir'] = '/usr/lib/apt-cacher-ng'
index 9cc405f..d373185 100644 (file)
@@ -1,5 +1,4 @@
 ---
-# $ fly -t $CC_TARGET sp -p apt_utils-cookbook -c concourse.yml -l fly-vars.yml -l ~/sec/credentials-prod.yml
 resources:
 - name: src-git
   type: git
@@ -8,8 +7,8 @@ resources:
     branch: master
     paths:
     - cookbooks/((cookbook-name))
-    git_user: ((git-user-osdn))
     private_key: ((git-private-key))
+    git_user: ((git-user-osdn))
   #check_every: 1h  # default: 1m
 - name: chefdk-cache
   type: docker-image
diff --git a/cookbooks/apt_utils/fly-vars.local.yml b/cookbooks/apt_utils/fly-vars.local.yml
new file mode 100644 (file)
index 0000000..9103055
--- /dev/null
@@ -0,0 +1,2 @@
+---
+cookbook-name: apt_utils
index 1c6b39b..76f2fcd 100644 (file)
@@ -1,3 +1,2 @@
 ---
-cookbook-name: apt_utils
 chefdk-version: 1.6.11
index c516342..74ba7e4 100644 (file)
@@ -15,6 +15,6 @@ chef_version     '>= 12'
 end
 
 depends 'apt'
-depends 'docker-grid', '>= 0.2.7'
+depends 'docker-grid', '>= 0.5.8'
 depends 'platform_utils', '>= 0.4.3'
-depends 'ssl_cert', '>= 0.4.1'
+depends 'ssl_cert', '>= 0.4.2'
index 90eba49..65c52b9 100644 (file)
@@ -2,7 +2,7 @@
 # Cookbook Name:: apt_utils
 # Recipe:: apt-cacher-ng
 #
-# Copyright 2013-2017, whitestar
+# Copyright 2013-2018, whitestar
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
 
 ::Chef::Recipe.send(:include, PlatformUtils::PackageUtils)
 
+conf = node['apt_utils']['apt-cacher-ng']
+
 if node['platform_family'] == 'debian'
   pkg = 'apt-cacher-ng'
   user_name = pkg
@@ -32,14 +34,14 @@ if node['platform_family'] == 'debian'
     supports status: true, restart: true, reload: false
   end
 
-  directory node['apt_utils']['apt-cacher-ng']['CacheDir'] do
+  directory conf['CacheDir'] do
     owner user_name
     group user_name
     mode '2755'
     recursive true
   end
 
-  directory node['apt_utils']['apt-cacher-ng']['LogDir'] do
+  directory conf['LogDir'] do
     owner user_name
     group user_name
     mode '2755'
@@ -54,3 +56,21 @@ if node['platform_family'] == 'debian'
     notifies :restart, "service[#{service_name}]"
   end
 end
+
+# autopilot
+srv = 'apt-cacher-ng'
+template "/etc/cron.d/#{srv}-local" do
+  source  "etc/cron.d/#{srv}-local"
+  owner 'root'
+  group 'root'
+  mode '0644'
+  action :delete unless conf['autopilot']['enabled']
+end
+
+template '/usr/local/bin/restart_acng-local' do
+  source  'usr/local/bin/restart_acng-local'
+  owner 'root'
+  group 'root'
+  mode '0755'
+  action :delete unless conf['autopilot']['enabled']
+end
index 64c468c..3be179e 100644 (file)
@@ -110,12 +110,14 @@ template config_file do
   mode '0644'
 end
 
-log <<-"EOM"
+log 'aptly-docker-compose post install message' do
+  message <<-"EOM"
 Note: You must execute the following command manually.
-  See #{doc_url}
-  * Start:
-    $ cd #{app_dir}
-    $ docker-compose up -d
-  * Stop
-    $ docker-compose down
+    See #{doc_url}
+    * Start:
+      $ cd #{app_dir}
+      $ docker-compose up -d
+    * Stop
+      $ docker-compose down
 EOM
+end
index 5c327e7..c46d3cf 100644 (file)
@@ -2,7 +2,7 @@
 # Cookbook Name:: apt_utils
 # Recipe Spec:: apt-cacher-ng_spec
 #
-# Copyright 2015-2017, whitestar
+# Copyright 2015-2018, whitestar
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -22,8 +22,8 @@ require_relative '../spec_helper'
 describe 'apt_utils::apt-cacher-ng' do
   let(:chef_run_on_non_debian) {
     ChefSpec::SoloRunner.new(platform: 'centos', version: '7.0') {|node|
-      node.set['apt_utils']['apt-cacher-ng']['CacheDir'] = '/path/to/cache'
-      node.set['apt_utils']['apt-cacher-ng']['LogDir'] = '/path/to/log'
+      node.normal['apt_utils']['apt-cacher-ng']['CacheDir'] = '/path/to/cache'
+      node.normal['apt_utils']['apt-cacher-ng']['LogDir'] = '/path/to/log'
     }.converge(described_recipe)
   }
 
@@ -39,8 +39,8 @@ describe 'apt_utils::apt-cacher-ng' do
 
   let(:chef_run_on_debian) {
     ChefSpec::SoloRunner.new(platform: 'debian', version: '8.1') {|node|
-      node.set['apt_utils']['apt-cacher-ng']['CacheDir'] = '/path/to/cache'
-      node.set['apt_utils']['apt-cacher-ng']['LogDir'] = '/path/to/log'
+      node.normal['apt_utils']['apt-cacher-ng']['CacheDir'] = '/path/to/cache'
+      node.normal['apt_utils']['apt-cacher-ng']['LogDir'] = '/path/to/log'
     }.converge(described_recipe)
   }
 
index 2b1bc9c..be1a37e 100644 (file)
@@ -2,7 +2,7 @@
 # Cookbook Name:: apt_utils
 # Recipe Spec:: source_spec
 #
-# Copyright 2015, whitestar
+# Copyright 2015-2018, whitestar
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ describe 'apt_utils::source' do
 
   let(:chef_run_on_debian) {
     ChefSpec::SoloRunner.new(platform: 'debian', version: '8.1') {|node|
-      node.set['apt_utils']['source']['apt_lines'] = [
+      node.normal['apt_utils']['source']['apt_lines'] = [
         'deb http://cdn.debian.net/debian jessie main contrib non-free',
         'deb http://cdn.debian.net/debian jessie-updates main contrib',
       ]
diff --git a/cookbooks/apt_utils/templates/default/etc/cron.d/apt-cacher-ng-local b/cookbooks/apt_utils/templates/default/etc/cron.d/apt-cacher-ng-local
new file mode 100644 (file)
index 0000000..8c38c0f
--- /dev/null
@@ -0,0 +1,25 @@
+<%
+name = 'apt-cacher-ng'
+
+conf = node['apt_utils']['apt-cacher-ng']['autopilot']
+log = "#{conf['log_dir']}/#{name}-local.log"
+wait_time = conf['wait_time']
+check_period = conf['check_period']
+
+cmd = nil
+st_cmd = nil
+if File.exist?('/etc/init.d/apt-cacher-ng')
+  cmd = '/etc/init.d/apt-cacher-ng restart'
+  st_cmd = '/etc/init.d/apt-cacher-ng status'
+else
+  cmd = 'systemctl restart apt-cacher-ng'
+  st_cmd = 'systemctl status apt-cacher-ng'
+end
+-%>
+SHELL=/bin/sh
+PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
+DATE_F='+%Y-%m-%d %H:%M:%S %z'
+LOG=<%= log %>
+
+@reboot root sleep <%= wait_time %> && date "$DATE_F" > $LOG && <%= cmd %> >> $LOG 2>&1 && <%= st_cmd %> >> $LOG 2>&1
+<%= check_period %> root /usr/local/bin/restart_acng-local
diff --git a/cookbooks/apt_utils/templates/default/usr/local/bin/restart_acng-local b/cookbooks/apt_utils/templates/default/usr/local/bin/restart_acng-local
new file mode 100644 (file)
index 0000000..ed1e02c
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/sh
+<%
+name = 'apt-cacher-ng'
+
+conf = node['apt_utils']['apt-cacher-ng']['autopilot']
+log = "#{conf['log_dir']}/#{name}-local.log"
+
+cmd = nil
+st_cmd = nil
+if File.exist?('/etc/init.d/apt-cacher-ng')
+  cmd = '/etc/init.d/apt-cacher-ng restart'
+  st_cmd = '/etc/init.d/apt-cacher-ng status'
+else
+  cmd = 'systemctl restart apt-cacher-ng'
+  st_cmd = 'systemctl status apt-cacher-ng'
+end
+-%>
+
+DATE_F='+%Y-%m-%d %H:%M:%S %z'
+LOG=<%= log %>
+
+date "$DATE_F" > $LOG
+<%= st_cmd %> | grep "Failed to start|Couldn't bind socket" && <%= cmd %> >> $LOG