【问题标题】:Can I create an AMI with packer on macOS?我可以在 macOS 上使用打包程序创建 AMI 吗?
【发布时间】:2017-09-30 03:07:15
【问题描述】:

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

我们混合使用 Ubuntu 和 Windows 服务器,其中大多数是 Ubuntu 服务器。我想在我的macbook上构建它。在生产中,我们使用 Jenkins。

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

我需要安装

  • IIS
  • 网站核心
  • 文件节拍

代码示例:

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”并未复制到服务器。

【问题讨论】:

    标签: amazon-web-services amazon-ami packer


    【解决方案1】:

    我已经从 Windows 主机构建了 Linux AMI,所以我对这个过程非常了解。它只是使用 AWS API 从源 AMI 创建实例,通过 SSH 进入并执行您想要的命令,将其关闭并为您存储新的 AMI(省略了一些细节)。因此,您使用哪个操作系统来创建 AMI 并不重要。

    但是我没有任何使用 WinRM 的经验,但根据这些文章,它看起来并不算太琐碎:

    two years 似乎已经支持 WinRM:

    2015 年 6 月 23 日 |米切尔桥本 |打包机

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

    Packer 0.8 的功能亮点:

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

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

    【讨论】:

    • 是的,我已经用我要开始工作的代码更新了我的问题
    【解决方案2】:

    Packer 在使用云构建器(例如amazon-ebs)构建时不依赖于您的操作系统。这是一个帮助您入门的工作示例,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 对您的操作系统没有依赖关系”是的,因为您使用的 winrm 不能在 mac 或 linux 上运行
    • @red888 这是不真实的。 Packer 使用 Go 原生 WinRM 库,该库将在 Go 编译到的任何操作系统上运行。 (我是 Packer 的维护者..)
    【解决方案3】:

    我实际上是通过使用 Packer 构建 OVA 来实现您正在寻找的结果,但我没有安装 vmware 工具,而是安装了 amazon cloud utils。这不是您正在寻找的,但我认为您可以通过这种方式获得预期的结果。

    然后,在安装了 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": [{ "Description": "Some_App", "UserBucket": { "S3Bucket": "centos-builds", "S3Key": "some-build-ami.ova"}}]}'
    3. aws ec2 describe-import-image-tasks

    一些假设 - 例如您的 EC2 密钥和访问密钥是环境变量,并且您在构建机器上安装了 AWS 工具。导入映像大约需要 15 分钟,然后您有一个不错的新鲜 AMI 在 ec2 中等待您启动。

    对于它的价值,这只是使用本地 vmware fusion builder 然后将其转换为 AMI,这是我在本地故障排除时需要的。

    More info here.

    【讨论】:

    • 这对您有帮助吗?
    猜你喜欢
    • 2022-01-23
    • 2018-07-14
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 2016-10-15
    • 2018-04-02
    • 2019-05-21
    • 1970-01-01
    相关资源
    最近更新 更多