diff --git a/builder/alicloud/ecs/builder.go b/builder/alicloud/ecs/builder.go index af40c92fc..8650dc2d1 100644 --- a/builder/alicloud/ecs/builder.go +++ b/builder/alicloud/ecs/builder.go @@ -5,6 +5,7 @@ package ecs import ( "log" + "fmt" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/config" @@ -98,8 +99,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe PrivateKeyFile: b.config.Comm.SSHPrivateKey, TemporaryKeyPairName: b.config.TemporaryKeyPairName, SSHAgentAuth: b.config.Comm.SSHAgentAuth, - //DebugKeyPath: b.config.Com - RegionId: b.config.AlicloudRegion, + DebugKeyPath: fmt.Sprintf("ecs_%s.pem", b.config.PackerBuildName), + RegionId: b.config.AlicloudRegion, }, } if b.chooseNetworkType() == VpcNet { diff --git a/builder/alicloud/ecs/step_attach_keypair.go b/builder/alicloud/ecs/step_attach_keypair.go index 551c74cdf..a6cc01381 100644 --- a/builder/alicloud/ecs/step_attach_keypair.go +++ b/builder/alicloud/ecs/step_attach_keypair.go @@ -7,6 +7,7 @@ import ( "github.com/denverdino/aliyungo/ecs" "github.com/hashicorp/packer/packer" "github.com/mitchellh/multistep" + "time" ) type stepAttachKeyPar struct { @@ -21,7 +22,7 @@ func (s *stepAttachKeyPar) Run(state multistep.StateBag) multistep.StepAction { client := state.Get("client").(*ecs.Client) config := state.Get("config").(Config) instance := state.Get("instance").(*ecs.InstanceAttributesType) - retry_times := 3 + timeoutPoint := time.Now().Add(120 * time.Second) for { err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion), KeyPairName: keyPairName, InstanceIds: "[\"" + instance.InstanceId + "\"]"}) @@ -29,8 +30,8 @@ func (s *stepAttachKeyPar) Run(state multistep.StateBag) multistep.StepAction { e, _ := err.(*common.Error) if (!(e.Code == "MissingParameter" || e.Code == "DependencyViolation.WindowsInstance" || e.Code == "InvalidKeyPairName.NotFound" || e.Code == "InvalidRegionId.NotFound")) && - retry_times > 0 { - retry_times = retry_times - 1 + time.Now().Before(timeoutPoint) { + time.Sleep(5 * time.Second) continue } err := fmt.Errorf("Error attaching keypair %s to instance %s : %s", diff --git a/builder/alicloud/ecs/step_config_security_group.go b/builder/alicloud/ecs/step_config_security_group.go index 6bfbb386a..dc8c083b2 100644 --- a/builder/alicloud/ecs/step_config_security_group.go +++ b/builder/alicloud/ecs/step_config_security_group.go @@ -121,12 +121,12 @@ func (s *stepConfigAlicloudSecurityGroup) Cleanup(state multistep.StateBag) { ui := state.Get("ui").(packer.Ui) message(state, "security group") - start := time.Now().Add(10 * time.Second) + timeoutPoint := time.Now().Add(120 * time.Second) for { if err := client.DeleteSecurityGroup(common.Region(s.RegionId), s.SecurityGroupId); err != nil { e, _ := err.(*common.Error) - if e.Code == "DependencyViolation" && time.Now().Before(start) { - time.Sleep(1 * time.Second) + if e.Code == "DependencyViolation" && time.Now().Before(timeoutPoint) { + time.Sleep(5 * time.Second) continue } ui.Error(fmt.Sprintf("Failed to delete security group, it may still be around: %s", err)) diff --git a/builder/alicloud/ecs/step_config_vpc.go b/builder/alicloud/ecs/step_config_vpc.go index 0dce325a4..c4bdd59a3 100644 --- a/builder/alicloud/ecs/step_config_vpc.go +++ b/builder/alicloud/ecs/step_config_vpc.go @@ -78,13 +78,13 @@ func (s *stepConfigAlicloudVPC) Cleanup(state multistep.StateBag) { ui := state.Get("ui").(packer.Ui) message(state, "VPC") - start := time.Now().Add(10 * time.Second) + timeoutPoint := time.Now().Add(60 * time.Second) for { if err := client.DeleteVpc(s.VpcId); err != nil { e, _ := err.(*common.Error) if (e.Code == "DependencyViolation.Instance" || e.Code == "DependencyViolation.RouteEntry" || e.Code == "DependencyViolation.VSwitch" || - e.Code == "DependencyViolation.SecurityGroup") && time.Now().Before(start) { + e.Code == "DependencyViolation.SecurityGroup") && time.Now().Before(timeoutPoint) { time.Sleep(1 * time.Second) continue } diff --git a/builder/alicloud/ecs/step_config_vswitch.go b/builder/alicloud/ecs/step_config_vswitch.go index 0705fb45a..734647e07 100644 --- a/builder/alicloud/ecs/step_config_vswitch.go +++ b/builder/alicloud/ecs/step_config_vswitch.go @@ -130,13 +130,13 @@ func (s *stepConfigAlicloudVSwitch) Cleanup(state multistep.StateBag) { client := state.Get("client").(*ecs.Client) ui := state.Get("ui").(packer.Ui) message(state, "vSwitch") - start := time.Now().Add(10 * time.Second) + timeoutPoint := time.Now().Add(10 * time.Second) for { if err := client.DeleteVSwitch(s.VSwitchId); err != nil { e, _ := err.(*common.Error) if (e.Code == "IncorrectVSwitchStatus" || e.Code == "DependencyViolation" || e.Code == "DependencyViolation.HaVip" || - e.Code == "IncorretRouteEntryStatus") && time.Now().Before(start) { + e.Code == "IncorretRouteEntryStatus") && time.Now().Before(timeoutPoint) { time.Sleep(1 * time.Second) continue } diff --git a/examples/alicloud/basic/alicloud.json b/examples/alicloud/basic/alicloud.json index 9c7384d98..d96cafa57 100644 --- a/examples/alicloud/basic/alicloud.json +++ b/examples/alicloud/basic/alicloud.json @@ -8,8 +8,8 @@ "access_key":"{{user `access_key`}}", "secret_key":"{{user `secret_key`}}", "region":"cn-beijing", - "image_name":"packer_basi", - "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd", + "image_name":"packer_basic", + "source_image":"centos_7_2_64_40G_base_20170222.vhd", "ssh_username":"root", "instance_type":"ecs.n1.tiny", "io_optimized":"true" @@ -18,7 +18,7 @@ "type": "shell", "inline": [ "sleep 30", - "apt-get update -yy" + "yum install redis.x86_64 -y" ] }] } diff --git a/examples/alicloud/basic/alicloud_windows.json b/examples/alicloud/basic/alicloud_windows.json index bb1f16773..6a530ef36 100644 --- a/examples/alicloud/basic/alicloud_windows.json +++ b/examples/alicloud/basic/alicloud_windows.json @@ -9,7 +9,7 @@ "secret_key":"{{user `secret_key`}}", "region":"cn-beijing", "image_name":"packer_test", - "source_image":"win2012_64_datactr_r2_en_40G_alibase_20160622.vhd", + "source_image":"win2008r2_64_ent_sp1_zh-cn_40G_alibase_20170622.vhd", "instance_type":"ecs.n1.tiny", "io_optimized":"true", "image_force_delete":"true", diff --git a/examples/alicloud/basic/alicloud_with_data_disk.json b/examples/alicloud/basic/alicloud_with_data_disk.json index 2d99af8fb..88412d98a 100644 --- a/examples/alicloud/basic/alicloud_with_data_disk.json +++ b/examples/alicloud/basic/alicloud_with_data_disk.json @@ -9,7 +9,7 @@ "secret_key":"{{user `secret_key`}}", "region":"cn-beijing", "image_name":"packer_with_data_disk", - "source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd", + "source_image":"centos_7_2_64_40G_base_20170222.vhd", "ssh_username":"root", "instance_type":"ecs.n1.tiny", "io_optimized":"true", @@ -19,7 +19,7 @@ "type": "shell", "inline": [ "sleep 30", - "apt-get update -yy" + "yum install redis.x86_64 -y" ] }] } diff --git a/examples/alicloud/chef/alicloud.json b/examples/alicloud/chef/alicloud.json index 8bfcfbc8b..c04e0600f 100644 --- a/examples/alicloud/chef/alicloud.json +++ b/examples/alicloud/chef/alicloud.json @@ -9,7 +9,7 @@ "secret_key":"{{user `secret_key`}}", "region":"cn-beijing", "image_name":"packer_chef2", - "source_image":"ubuntu_14_0405_64_40G_base_20170222.vhd", + "source_image":"ubuntu_14_0405_64_40G_alibase_20170625.vhd", "ssh_username":"root", "instance_type":"ecs.n1.medium", "io_optimized":"true", diff --git a/examples/alicloud/chef/chef.sh b/examples/alicloud/chef/chef.sh index 6d7ecac86..6d678b7cc 100644 --- a/examples/alicloud/chef/chef.sh +++ b/examples/alicloud/chef/chef.sh @@ -1,4 +1,5 @@ #!/bin/sh +#if the related deb pkg not found, please replace with it other avaiable repository url HOSTNAME=`ifconfig eth1|grep 'inet addr'|cut -d ":" -f2|cut -d " " -f1` if [ not $HOSTNAME ] ; then HOSTNAME=`ifconfig eth0|grep 'inet addr'|cut -d ":" -f2|cut -d " " -f1` diff --git a/examples/alicloud/jenkins/alicloud.json b/examples/alicloud/jenkins/alicloud.json new file mode 100644 index 000000000..481c39b0a --- /dev/null +++ b/examples/alicloud/jenkins/alicloud.json @@ -0,0 +1,31 @@ +{ + "variables": { + "access_key": "{{env `ALICLOUD_ACCESS_KEY`}}", + "secret_key": "{{env `ALICLOUD_SECRET_KEY`}}" + }, + "builders": [{ + "type":"alicloud-ecs", + "access_key":"{{user `access_key`}}", + "secret_key":"{{user `secret_key`}}", + "region":"cn-beijing", + "image_name":"packer_jenkins", + "source_image":"ubuntu_14_0405_64_40G_alibase_20170625.vhd", + "ssh_username":"root", + "instance_type":"ecs.n1.medium", + "io_optimized":"true", + "image_force_delete":"true", + "ssh_password":"Test12345" + }], + "provisioners": [{ + "type": "file", + "source": "examples/alicloud/jenkins/jenkins.sh", + "destination": "/root/" + },{ + "type": "shell", + "inline": [ + "cd /root/", + "chmod 755 jenkins.sh", + "./jenkins.sh" + ] + }] +} diff --git a/examples/alicloud/jenkins/jenkins.sh b/examples/alicloud/jenkins/jenkins.sh new file mode 100644 index 000000000..af3eb51a3 --- /dev/null +++ b/examples/alicloud/jenkins/jenkins.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +JENKINS_URL='http://mirrors.jenkins.io/war-stable/2.32.2/jenkins.war' + +TOMCAT_VERSION='7.0.77' +TOMCAT_NAME="apache-tomcat-$TOMCAT_VERSION" +TOMCAT_PACKAGE="$TOMCAT_NAME.tar.gz" +TOMCAT_URL="http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v$TOMCAT_VERSION/bin/$TOMCAT_PACKAGE" +TOMCAT_PATH="/opt/$TOMCAT_NAME" + +#install jdk +if grep -Eqi "Ubuntu|Debian|Raspbian" /etc/issue || grep -Eq "Ubuntu|Debian|Raspbian" /etc/*-release; then + sudo apt-get update -y + sudo apt-get install -y openjdk-7-jdk +elif grep -Eqi "CentOS|Fedora|Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "CentOS|Fedora|Red Hat Enterprise Linux Server" /etc/*-release; then + sudo yum update -y + sudo yum install -y openjdk-7-jdk +else + echo "Unknown OS type." +fi + +#install jenkins server +mkdir ~/work +cd ~/work + +#install tomcat +wget $TOMCAT_URL +tar -zxvf $TOMCAT_PACKAGE +mv $TOMCAT_NAME /opt + +#install +wget $JENKINS_URL +mv jenkins.war $TOMCAT_PATH/webapps/ + +#set emvironment +echo "TOMCAT_PATH=\"$TOMCAT_PATH\"">>/etc/profile +echo "JENKINS_HOME=\"$TOMCAT_PATH/webapps/jenkins\"">>/etc/profile +echo PATH="\"\$PATH:\$TOMCAT_PATH:\$JENKINS_HOME\"">>/etc/profile +. /etc/profile + +#start tomcat & jenkins +$TOMCAT_PATH/bin/startup.sh + +#set start on boot +sed -i "/#!\/bin\/sh/a$TOMCAT_PATH/bin/startup.sh" /etc/rc.local + +#clean +rm -rf ~/work diff --git a/examples/alicloud/local/centos.json b/examples/alicloud/local/centos.json new file mode 100644 index 000000000..45c19b3dc --- /dev/null +++ b/examples/alicloud/local/centos.json @@ -0,0 +1,60 @@ +{"variables": { + "box_basename": "centos-6.8", + "build_timestamp": "{{isotime \"20060102150405\"}}", + "cpus": "1", + "disk_size": "4096", + "git_revision": "__unknown_git_revision__", + "headless": "", + "http_proxy": "{{env `http_proxy`}}", + "https_proxy": "{{env `https_proxy`}}", + "iso_checksum": "0ca12fe5f28c2ceed4f4084b41ff8a0b", + "iso_checksum_type": "md5", + "iso_name": "CentOS-6.8-x86_64-minimal.iso", + "ks_path": "centos-6.8/ks.cfg", + "memory": "512", + "metadata": "floppy/dummy_metadata.json", + "mirror": "http://mirrors.aliyun.com/centos", + "mirror_directory": "6.8/isos/x86_64", + "name": "centos-6.8", + "no_proxy": "{{env `no_proxy`}}", + "template": "centos-6.8-x86_64", + "version": "2.1.TIMESTAMP" + }, + "builders":[ + { + "boot_command": [ + " text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/{{user `ks_path`}}" + ], + "boot_wait": "10s", + "disk_size": "{{user `disk_size`}}", + "headless": "{{ user `headless` }}", + "http_directory": "http", + "iso_checksum": "{{user `iso_checksum`}}", + "iso_checksum_type": "{{user `iso_checksum_type`}}", + "iso_url": "{{user `mirror`}}/{{user `mirror_directory`}}/{{user `iso_name`}}", + "output_directory": "packer-{{user `template`}}-qemu", + "shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p", + "ssh_password": "vagrant", + "ssh_port": 22, + "ssh_username": "root", + "ssh_wait_timeout": "10000s", + "type": "qemu", + "vm_name": "{{ user `template` }}.raw", + "net_device": "virtio-net", + "disk_interface": "virtio", + "format": "raw" + } + ], +"post-processors":[ + { + "type":"alicloud-import", + "oss_bucket_name": "packer", + "image_name": "packer_import", + "image_os_type": "linux", + "image_platform": "CentOS", + "image_architecture": "x86_64", + "image_system_size": "40", + "region":"cn-beijing" + } + ] +} diff --git a/examples/alicloud/local/http/centos-6.8/ks.cfg b/examples/alicloud/local/http/centos-6.8/ks.cfg new file mode 100644 index 000000000..90864b006 --- /dev/null +++ b/examples/alicloud/local/http/centos-6.8/ks.cfg @@ -0,0 +1,69 @@ +install +cdrom +lang en_US.UTF-8 +keyboard us +network --bootproto=dhcp +rootpw vagrant +firewall --disabled +selinux --permissive +timezone UTC +unsupported_hardware +bootloader --location=mbr +text +skipx +zerombr +clearpart --all --initlabel +autopart +auth --enableshadow --passalgo=sha512 --kickstart +firstboot --disabled +reboot +user --name=vagrant --plaintext --password vagrant +key --skip + +%packages --nobase --ignoremissing --excludedocs +# vagrant needs this to copy initial files via scp +openssh-clients +sudo +kernel-headers +kernel-devel +gcc +make +perl +wget +nfs-utils +-fprintd-pam +-intltool + +# unnecessary firmware +-aic94xx-firmware +-atmel-firmware +-b43-openfwwf +-bfa-firmware +-ipw2100-firmware +-ipw2200-firmware +-ivtv-firmware +-iwl100-firmware +-iwl1000-firmware +-iwl3945-firmware +-iwl4965-firmware +-iwl5000-firmware +-iwl5150-firmware +-iwl6000-firmware +-iwl6000g2a-firmware +-iwl6050-firmware +-libertas-usb8388-firmware +-ql2100-firmware +-ql2200-firmware +-ql23xx-firmware +-ql2400-firmware +-ql2500-firmware +-rt61pci-firmware +-rt73usb-firmware +-xorg-x11-drv-ati-firmware +-zd1211-firmware + +%post +# Force to set SELinux to a permissive mode +sed -i -e 's/\(^SELINUX=\).*$/\1permissive/' /etc/selinux/config +# sudo +echo "%vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/vagrant