简体   繁体   English

我可以在macOS上使用打包器创建AMI吗?

[英]Can I create an AMI with packer on macOS?

I am trying to build a Windows AMI (Custom AWS Image) with Packer. 我正在尝试使用Packer构建Windows AMI(自定义AWS映像)。 Is there a way to use WinRM on macOS, or do I have to build the image on a windows machine? 有没有办法在macOS上使用WinRM,还是我必须在Windows机器上构建图像?

We are using a mix of Ubuntu and Windows servers and the majority are Ubuntu servers. 我们使用的是Ubuntu和Windows服务器,大多数是Ubuntu服务器。 I would like to build it all on my macbook. 我想在我的macbook上构建它。 In production we are using Jenkins. 在制作中我们使用Jenkins。

The purpose of the image is to run IIS and Sitecore. 图像的目的是运行IIS和Sitecore。

I need to install 我需要安装

  • IIS IIS
  • Sitecore Sitecore的
  • Filebeat Filebeat

Code example: 代码示例:

resource "aws_key_pair" "mykey" {
  key_name = "mykey"
  public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
}

resource "aws_instance" "win-example" {
  security_groups = [ "${aws_security_group.windows-admin.id}" ]
  subnet_id = "subnet-730c9c16"
  ami = "ami-40003a26"
  instance_type = "t2.micro"
  associate_public_ip_address = true
  key_name = "${aws_key_pair.mykey.key_name}"
  tags {
    Name = "win-example"
  }
  user_data = <<EOF
<powershell>
net user ${var.INSTANCE_USERNAME} ${var.INSTANCE_PASSWORD} /add
net localgroup administrators ${var.INSTANCE_USERNAME} /add

winrm quickconfig -q
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="300"}'
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow

net stop winrm
sc.exe config winrm start=auto
net start winrm
</powershell>
EOF

  provisioner "file" {
    source = "test.txt"
    destination = "C:/test.txt"
    connection {
      type = "winrm"
      user = "${var.INSTANCE_USERNAME}"
      password = "${var.INSTANCE_PASSWORD}"
      insecure = true
      timeout = "10m"
    }
  }
  connection {
    type = "winrm"
    user = "${var.INSTANCE_USERNAME}"
    password = "${var.INSTANCE_PASSWORD}"
    insecure = true
    timeout = "10m"
  }
}

terraform reports an error: terraform报告错误:

Error applying plan:
1 error(s) occurred:
* aws_instance.win-example: 1 error(s) occurred:
* unknown error Post http://54.229.229.22:5985/wsman: dial tcp 54.229.229.22:5985: getsockopt: operation timed out

The powershell script runs and the user is created but the file "test.txt" are not copied to the server. powershell脚本运行并创建用户,但文件“test.txt”未复制到服务器。

I have built Linux AMIs from a Windows host, so I know that much about the process. 我已经从Windows主机构建了Linux AMI,所以我对这个过程了解很多。 It just uses AWS APIs to create the instance from a source AMI, SSHs in and executes the commands you want, shuts it down and stores the new AMI for you (with some details left out). 它只使用AWS API从源AMI创建实例,SSH并执行所需的命令,关闭它并为您存储新的AMI(省略一些细节)。 So it shouldn't matter which OS you use to create the AMI. 因此,使用哪个操作系统创建AMI无关紧要。

However I don't have any experience with WinRM, but based on these articles it doesn't look too trivial: 但是我对WinRM没有任何经验,但基于这些文章,它看起来并不是那么简单:

WinRM seems to have been supported for two years already: WinRM似乎已经支持了两年

JUN 23 2015 | 2015年6月23日| MITCHELL HASHIMOTO | MITCHELL HASHIMOTO | PACKER 盒包

We've released Packer 0.8. 我们发布了Packer 0.8。 Packer is a tool for building virtual machine images, containers, and other deployable artifacts. Packer是用于构建虚拟机映像,容器和其他可部署工件的工具。

Feature highlights for Packer 0.8: Packer 0.8的功能亮点:

  • WinRM and Windows Provisioners WinRM和Windows配置程序
  • Windows AWS Images Windows AWS图像

Have you actually tried building Windows images and faced some issues? 您是否真的尝试构建Windows映像并遇到一些问题?

Packer has no dependnecies on you OS when building with a cloud builder (eg amazon-ebs ). 在使用云构建器(例如amazon-ebs )构建时,Packer对您的操作系统没有依赖性。 This is a working example to get you started, template.json : 这是一个让你入门的工作示例, template.json

{
  "builders": [{
    "type": "amazon-ebs",
    "region": "eu-west-1",
    "instance_type": "m3.medium",
    "source_ami": "ami-d593bba6",
    "ami_name": "packer-demo-{{timestamp}}",
    "user_data_file": "userdata.txt",
    "communicator": "winrm",
    "winrm_username": "Administrator"
  }],
  "provisioners": [{
    "type": "powershell",
    "inline": [
      "dir c:\\"
    ]
  }]
}

And userdata.txt : userdata.txt

<powershell>
winrm quickconfig -q
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="300"}'
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow

net stop winrm
sc config winrm start=auto
net start winrm

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope LocalMachine
</powershell>

I actually achieve the outcome you are looking for by building an OVA with Packer, but instead of installing vmware tools, I install the amazon cloud utils. 我实际上通过使用Packer构建OVA来实现您正在寻找的结果,但是我安装了亚马逊云工具,而不是安装vmware工具。 This isn't exactly what you are looking for, but I think you can get the desired outcome this way. 这不是你想要的,但我认为你可以通过这种方式获得理想的结果。

Then, from the command line (or my build script), with the AWS tools installed I do the following: 然后,从命令行(或我的构建脚本),安装AWS工具,我执行以下操作:

  1. aws s3 cp my-machine.ova s3://some-folder/mymachine.ova aws s3 cp my-machine.ova s​​3://some-folder/mymachine.ova
  2. aws ec2 import-image --cli-input-json '{ "Platform": "Linux", "Architecture": "x86_64", "Description": "Some Centos AMI v21.2.1", "DiskContainers": [{ "Description": "Some_App", "UserBucket": { "S3Bucket": "centos-builds", "S3Key": "some-build-ami.ova"}}]}' aws ec2 import-image --cli-input-json'{“Platform”:“Linux”,“Architecture”:“x86_64”,“Description”:“Some Centos AMI v21.2.1”,“DiskContainers”:[{“描述“:”Some_App“,”UserBucket“:{”S3Bucket“:”centos-builds“,”S3Key“:”some-build-ami.ova“}}}}'
  3. aws ec2 describe-import-image-tasks aws ec2 describe-import-image-tasks

A few assumptions - such as your EC2 Secret and Access Key are environment variable, and that you have the AWS tools installed on your build machine. 一些假设 - 例如您的EC2 Secret和Access Key是环境变量,并且您在构建计算机上安装了AWS工具。 The import image takes about 15 minutes, then you have a nice fresh AMI waiting for you up in ec2. 导入图像大约需要15分钟,然后你有一个漂亮的新AMI在ec2等你。

For what it's worth, this just uses the local vmware fusion builder then converts it into an AMI, which is desired by me for local troubleshooting. 对于它的价值,这只是使用本地vmware融合构建器然后将其转换为AMI,我希望本地故障排除。

More info here. 更多信息在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM