ansible-galaxyめも

いっつも忘れるので

  • foo-roleってディレクトリつくって、そこにansible-glaxy initする
    • cd foo-role
    • export ROLE_PATH=$(echo $(pwd) |awk 'BEGIN {FS="/";OFS="/"} {$NF="";print $0}');ansible-galaxy init foo-role e -f -p "$ROLE_PATH"
  • requirements.ymlに記述されているroleを手元のroleディレクトリに取ってきてinstall
    • ansible-galaxy install -f -r requirements.yml --roles-path=./roles

AWS CLIとYAMLでEC2インスタンスをつくるメモ

foo.yaml

AWSTemplateFormatVersion: "2010-09-09"
Description: "Foo Server Template"

Parameters:
  UserDefinedHostName:
    Type: String

Resources:
  MyEC2Instance: #An inline comment
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: "ami-8422ebe2" # Ubuntu xenial hvm:ebs-ssd
      InstanceType: t2.micro
      BlockDeviceMappings:
        - DeviceName: /dev/sdm
          Ebs:
            VolumeType: gp2
            Iops: 200
            DeleteOnTermination: True
            VolumeSize: 8
      SubnetId: "subnet-xxx"
      KeyName: "xxx"
      SecurityGroupIds:
        - "sg-xxx"
        - "sg-xxx"
      Tags:
        - Key: "Name"
          Value: !Ref UserDefinedHostName
      UserData: !Base64 |
      #!/bin/bash
      apt update
      apt install ansible -y

AWS CLI

aws cloudformation create-stack --stack-name foo-server --template-body file://foo.yml --parameters ParameterKey="UserDefinedHostName",ParameterValue="foo-server"

unicorn.logをlogrotate.dでローテするメモ

基本的にはUSR1シグナルを送れば良い

sample

/var/log/foo/bar/unicorn.log {
  daily #毎日
  missingok # ログファイルがなくても処理をつづける
  rotate 7 #世代
  dateext #ファイルを日付形式に
  compress #圧縮する
  delaycompress #圧縮は次のローテまで遅らせる

  lastaction
    pid=/foo/bar/unicorn.pid
    test -s $pid && kill -USR1 "$(cat $pid)"
  endscript
}

参考

Jenkins2.xで、jobの実行ユーザを取得するgroovyスニペット

println currentBuild.getRawBuild().getCause(hudson.model.Cause$UserIdCause).getUserName()

めんどいのぅ

やりたいことはこっちでもできた(すこし短くなった)

currentBuild.build().getCause(Cause.UserIdCause.class).getUserName()

更に、.classも省略できる

currentBuild.build().getCause(Cause.UserIdCause).getUserName()

複数サーバの設定をserverspecに食わせて実行するサンプル

dev.yaml

"host1":
  :roles:
    - foocheck
  :ssh_opts:
    :user: centos
    :keys: ~/.ssh/bar.pem
    :paranoid: false
"host2":
  :roles:
    - foocheck
  :ssh_opts:
    :user: centos
    :keys: ~/.ssh/bar.pem
    :paranoid: false

Rakefile

require 'rake'
require 'rspec/core/rake_task'
require 'yaml'

task :spec    => 'spec:all'
task :default => :spec


namespace :spec do
  spec_env = ENV['SPEC_ENV']
  if spec_env
    properties = YAML.load_file("./#{spec_env}.yaml")
  else
    raise RuntimeError, "HPB-ERROR: No hosts defined for \"#{spec_env}\""
  end

  task :all => properties.keys.map {|key| 'spec:' + key }

  properties.each_key do |key|
    desc "Run serverspec to #{key}"
    RSpec::Core::RakeTask.new(key.to_sym) do |t|
      t.fail_on_error = false
      ENV['TARGET_HOST'] = key
      t.pattern = 'spec/{' + properties[key][:roles].join(',') + '}/*_spec.rb'
    end
  end

end

spec_helper.rb

require 'serverspec'
require 'net/ssh'

set :backend, :ssh

if ENV['ASK_SUDO_PASSWORD']
  begin
    require 'highline/import'
  rescue LoadError
    fail "highline is not available. Try installing it."
  end
  set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
else
  set :sudo_password, ENV['SUDO_PASSWORD']
end

host = ENV['TARGET_HOST']
options = Net::SSH::Config.for(host)

options[:user] ||= Etc.getlogin

# add ssh options
spec_env = ENV['SPEC_ENV']
properties = YAML.load_file("./#{spec_env}.yaml")
unless properties[host][:ssh_opts].nil?
  options.merge!(properties[host][:ssh_opts])
end

# add pty option
set :request_pty, true

set :host,        options[:host_name] || host
set :ssh_options, options

# Disable sudo
# set :disable_sudo, true


# Set environment variables
# set :env, :LANG => 'C', :LC_MESSAGES => 'C'

# Set PATH
# set :path, '/sbin:/usr/local/sbin:$PATH'

実行方法

SPEC_ENV=dev /usr/local/rbenv/shims/bundle exec rake spec:all -t