繁体   English   中英

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

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

我正在尝试使用Packer构建Windows AMI(自定义AWS映像)。 有没有办法在macOS上使用WinRM,还是我必须在Windows机器上构建图像?

我们使用的是Ubuntu和Windows服务器,大多数是Ubuntu服务器。 我想在我的macbook上构建它。 在制作中我们使用Jenkins。

图像的目的是运行IIS和Sitecore。

我需要安装

  • IIS
  • Sitecore的
  • Filebeat

代码示例:

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报告错误:

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

powershell脚本运行并创建用户,但文件“test.txt”未复制到服务器。

我已经从Windows主机构建了Linux AMI,所以我对这个过程了解很多。 它只使用AWS API从源AMI创建实例,SSH并执行所需的命令,关闭它并为您存储新的AMI(省略一些细节)。 因此,使用哪个操作系统创建AMI无关紧要。

但是我对WinRM没有任何经验,但基于这些文章,它看起来并不是那么简单:

WinRM似乎已经支持了两年

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

我们发布了Packer 0.8。 Packer是用于构建虚拟机映像,容器和其他可部署工件的工具。

Packer 0.8的功能亮点:

  • WinRM和Windows配置程序
  • Windows AWS图像

您是否真的尝试构建Windows映像并遇到一些问题?

在使用云构建器(例如amazon-ebs )构建时,Packer对您的操作系统没有依赖性。 这是一个让你入门的工作示例, 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:\\"
    ]
  }]
}

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>

我实际上通过使用Packer构建OVA来实现您正在寻找的结果,但是我安装了亚马逊云工具,而不是安装vmware工具。 这不是你想要的,但我认为你可以通过这种方式获得理想的结果。

然后,从命令行(或我的构建脚本),安装AWS工具,我执行以下操作:

  1. 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”:[{“描述“:”Some_App“,”UserBucket“:{”S3Bucket“:”centos-builds“,”S3Key“:”some-build-ami.ova“}}}}'
  3. aws ec2 describe-import-image-tasks

一些假设 - 例如您的EC2 Secret和Access Key是环境变量,并且您在构建计算机上安装了AWS工具。 导入图像大约需要15分钟,然后你有一个漂亮的新AMI在ec2等你。

对于它的价值,这只是使用本地vmware融合构建器然后将其转换为AMI,我希望本地故障排除。

更多信息在这里。

暂无
暂无

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

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