Skip to content

Commit

Permalink
install_strategy for install.sh
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Wright <[email protected]>
  • Loading branch information
Patrick Wright committed Aug 16, 2017
1 parent ff4390e commit d52b50c
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
nodes/
tmp/

# Terraform
*.tfstate
*.tfstate.backup
*_override.tf

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name 'acceptance-cookbook'

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
execute "terraform destroy -force" do
cwd "#{node['chef-acceptance']['suite-dir']}/terraform"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
execute "bundle exec ruby -e \"require 'mixlib/install'; puts Mixlib::Install.new(product_name: 'chef', product_version: :latest, channel: :stable, install_command_options: {install_strategy: 'once'}).install_command\" > ../.acceptance_data/ubuntu_install_command_once.sh" do
cwd node['chef-acceptance']['suite-dir']
end

execute "terraform plan" do
cwd "#{node['chef-acceptance']['suite-dir']}/terraform"
end

execute "terraform apply" do
cwd "#{node['chef-acceptance']['suite-dir']}/terraform"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ruby_block "get ip" do
block do
tf_state = JSON.parse(File.read("#{node['chef-acceptance']['suite-dir']}/terraform/terraform.tfstate"))
node.default["ip"] = tf_state["modules"].first["resources"]["aws_instance.mixlib_install_sh"]["primary"]["attributes"]["public_ip"]
end
end

execute "run inspec" do
command lazy { "inspec exec verify.rb -t ssh://ubuntu@#{node['ip']} -i ~/.ssh/es-infrastructure.pem" }
cwd "#{node['chef-acceptance']['suite-dir']}/inspec"
end
7 changes: 7 additions & 0 deletions acceptance/ubuntu_install_command_once/inspec/verify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe package("chef") do
it { should be_installed }
end

describe command("/tmp/install.sh") do
its("stdout") { should match "Nothing to install" }
end
78 changes: 78 additions & 0 deletions acceptance/ubuntu_install_command_once/terraform/application.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
data "aws_ami" "ubuntu_14_ami" {
most_recent = true

filter {
name = "owner-id"
values = ["099720109477"]
}

filter {
name = "name"
values = ["ubuntu/images/*/ubuntu-*-14.04-*-server-*"]
}

filter {
name = "architecture"
values = ["x86_64"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

filter {
name = "block-device-mapping.volume-type"
values = ["gp2"]
}

filter {
name = "image-type"
values = ["machine"]
}
}

resource "aws_instance" "mixlib_install_sh" {
count = 1

ami = "${data.aws_ami.ubuntu_14_ami.id}"
instance_type = "${var.aws_instance_type}"
key_name = "es-infrastructure"

associate_public_ip_address = true

subnet_id = "subnet-11ac0174" # Planet Releng Public Subnet
source_dest_check = false

vpc_security_group_ids = [
"sg-96274af3",
]

connection {
user = "ubuntu"
private_key = "${file("${var.connection_private_key}")}"
agent = "${var.connection_agent}"
timeout = "10m"
}

tags {
# ChefOps's AWS standard tags:
X-Dept = "EngServ"
X-Contact = "pwright"
X-Production = "false"
X-Environment = "acceptance"
X-Application = "mixlib-install"
}

provisioner "file" {
source = "../../.acceptance_data/ubuntu_install_command_once.sh"
destination = "/tmp/install.sh"
}

provisioner "remote-exec" {
inline = [
"chmod +x /tmp/install.sh",
"sudo bash /tmp/install.sh",
]
}
}
8 changes: 8 additions & 0 deletions acceptance/ubuntu_install_command_once/terraform/aws.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Restrict operation of terraform to chef-es profile so that
# we do not create resources in other aws profiles.
# We assume user has configured standard aws credentials
# under ~/.aws/credentials or with $AWS_SHARED_CREDENTIALS_FILE
provider "aws" {
region = "${var.aws_region}"
profile = "chef-aws"
}
27 changes: 27 additions & 0 deletions acceptance/ubuntu_install_command_once/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Region to create infrastructure in
variable "aws_region" {
type = "string"
default = "us-west-2"
}

variable "aws_instance_type" {
type = "string"
default = "t2.micro"
}

# Used to indicidate whether the environment should be treated as "prod"
# This is mainly used for the `X-Production` AWS tag.
variable "production" {
default = "false"
}

# SSH Connection info used for remote provisioning instances
variable "connection_agent" {
description = "Set to false to disable using ssh-agent to authenticate"
default = false
}

variable "connection_private_key" {
description = "File path to AWS keypair private key to provision with"
default = "~/.ssh/es-infrastructure.pem"
}
2 changes: 2 additions & 0 deletions lib/mixlib/install/generator/bourne.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def self.install_sh(context)
install_command = []
install_command << get_script("helpers.sh", context)
install_command << get_script("script_cli_parameters.sh")
install_command << get_script("check_product.sh")
install_command << get_script("platform_detection.sh")
install_command << get_script("fetch_metadata.sh", context)
install_command << get_script("fetch_package.sh")
Expand All @@ -44,6 +45,7 @@ def install_command
install_command = []
install_command << get_script("helpers.sh", user_agent_headers: options.user_agent_headers)
install_command << render_variables
install_command << get_script("check_product.sh")
install_command << get_script("platform_detection.sh")
install_command << get_script("fetch_metadata.sh")
install_command << get_script("fetch_package.sh")
Expand Down
6 changes: 6 additions & 0 deletions lib/mixlib/install/generator/bourne/scripts/check_product.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if test -d "/opt/$project" && test "x$install_strategy" = "xonce"; then
echo "$project installation detected"
echo "install_strategy set to 'once'"
echo "Nothing to install"
exit
fi
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
# $project: Project to be installed
# $cmdline_filename: Name of the package downloaded on local disk.
# $cmdline_dl_dir: Name of the directory downloaded package will be saved to on local disk.
# $install_strategy: Method of package installations. default strategy is to always install upon exec. Set to "once" to skip if project is installed
############

# Defaults
channel="stable"
project="chef"

while getopts pnv:c:f:P:d: opt
while getopts pnv:c:f:P:d:s opt
do
case "$opt" in

Expand All @@ -26,9 +27,10 @@ do
f) cmdline_filename="$OPTARG";;
P) project="$OPTARG";;
d) cmdline_dl_dir="$OPTARG";;
s) install_strategy="$OPTARG";;
\?) # unknown flag
echo >&2 \
"usage: $0 [-P project] [-c release_channel] [-v version] [-f filename | -d download_dir]"
"usage: $0 [-P project] [-c release_channel] [-v version] [-f filename | -d download_dir] [-s install_strategy]"
exit 1;;
esac
done
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/mixlib/install/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
cmdline_dl_dir: "/hereiam",
download_url_override: "https://packages.chef.ioo/files/stable/chef/12.19.36/debian/8/chef_12.19.36-1_amd64.deb",
checksum: "292651ac21e093a40446da6b9a9b075ad31be6991a6f7ab63d5b6c2edabaa03c",
install_strategy: "once",
}
end

Expand All @@ -225,6 +226,10 @@
it "adds checksum var" do
expect(install_script).to match(/checksum='#{install_command_options[:checksum]}'/)
end

it "adds checksum var" do
expect(install_script).to match(/install_strategy='#{install_command_options[:install_strategy]}'/)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/unit/mixlib/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
end

it "should render a script with cli parameters" do
expect(install_sh).to include("while getopts pnv:c:f:P:d: opt")
expect(install_sh).to include("while getopts pnv:c:f:P:d:s opt")
end

context "with custom base_url" do
Expand Down

0 comments on commit d52b50c

Please sign in to comment.