Skip to content

Instantly share code, notes, and snippets.

@thinkAmi
Created May 16, 2013 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkAmi/5590671 to your computer and use it in GitHub Desktop.
Save thinkAmi/5590671 to your computer and use it in GitHub Desktop.
NSEG #39 で発表したときの各種ソースコードです。 ・default.rb:Chefのレシピ ・Vagrantfile:Vagrantの設定ファイル ・vagrant.ps1:Jenkinsに実行させるPowerShellファイル
#
# Cookbook Name:: piyo
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
# 「update package index」の処理を記載
execute "update package index" do
command "apt-get update"
ignore_failure true
action :nothing
end.run_action(:run)
# 「ubuntu-desktop」パッケージをインストールし、起動する
package "apache2" do
action :install
end
service "apache2" do
action :start
end
# PowerShellは、セキュリティポリシーを変更した上で実行する
# See:
# http://www.atmarkit.co.jp/fwin2k/win2ktips/1023ps1sec/ps1sec.html
# 定数宣言
# Twilioの環境に応じて、以下のURLを変更する
Set-Variable URL "<TwilioURL>" -option Constant
# タイムスタンプのフォルダ作成&移動
# See: http://blog.livedoor.jp/akf0/archives/51323696.html
$datetime = Get-Date -format yyyyMMddhhmmss
Write-Host ("`r`n" + "[" + $datetime + "]" + "`r`n")
cd d:\
mkdir $datetime
cd $datetime
Write-Host ("`r`n" + "[clone repository]" + "`r`n")
# ChefのGitリポジトリ(d:\chef-repo)のブランチ"nseg39"をclone
# HEADを使うなら、「-b nseg39」を削除する
git clone -b nseg39 d:\chef-repo
# VagrantのGitリポジトリをクローン
# *基本的に変更がないので、最新のものをコピーする
copy d:\chef-vagrant\Vagrantfile .
# VMの起動
Write-Host ("`r`n" + "[vagrant up]" + "`r`n")
cmd /C "vagrant up"
# Chefの準備
# Start-Processではコケるので、一度コマンドプロンプトを起動してから、そちらにコマンドを渡す
Write-Host ("`r`n" + "[knife solo prepare]" + "`r`n")
cmd /C "knife solo prepare vagrant@192.168.33.11 -P vagrant"
# VMの再起動とrecipeの実行
Write-Host ("`r`n" + "[vagrant reload]" + "`r`n")
cmd /C "vagrant reload"
# 終了時刻
$endtime = Get-Date -format yyyyMMddhhmmss
Write-Host ("`r`n" + "[" + $endtime + "]" + "`r`n")
# 仮想環境構築が完了したため、シャットダウン
Write-Host ("`r`n" + "[vagrant halt]" + "`r`n")
cmd /C "vagrant halt"
# 終わったので、Twilioにて連絡
$webRequest = [System.Net.WebRequest]::Create($URL)
$webRequest.ContentType = "text/html"
$webRequest.ContentLength = 0
$webRequest.Method = "POST"
$response = $webRequest.GetResponse()
# -*- mode: ruby -*-
# vi: set ft=ruby :
$basedir = File.dirname(__FILE__)
# knife solo prepareしたか
def prepared?
File.exists? "#{$basedir}/prepared.txt"
end
# recipeの適用が終わっているか
def applied?
File.exists? "#{$basedir}/applied.txt"
end
Vagrant.configure("2") do |config|
# Box名
config.vm.box = "hoge"
# knife soloで使用するSSHのためのIPアドレス設定
config.vm.network :private_network, ip: "192.168.33.11"
# レシピの実行
if applied?
elsif prepared?
File.open("applied.txt", "w")
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = "./chef-repo/cookbooks"
chef.add_recipe "piyo"
end
else
File.open("prepared.txt", "w")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment