generated from QubitPi/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Webservice Packer plugin (#26)
- Loading branch information
Showing
10 changed files
with
422 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
Type: `webservice` | ||
|
||
<!-- | ||
Include a short description about the provisioner. This is a good place | ||
to call out what the provisioner does, and any additional text that might | ||
be helpful to a user. See https://www.packer.io/docs/provisioners/null | ||
--> | ||
|
||
The `webservice` provisioner is used to install Jersey-Jetty webservice WAR file in AWS AMI image | ||
|
||
|
||
<!-- Provisioner Configuration Fields --> | ||
|
||
**Required** | ||
|
||
- `warSource` (string) - The path to a local WAR file to upload to the machine. The path can be absolute or relative. If | ||
it is relative, it is relative to the working directory when Packer is executed. | ||
|
||
|
||
<!-- | ||
Optional Configuration Fields | ||
|
||
Configuration options that are not required or have reasonable defaults | ||
should be listed under the optionals section. Defaults values should be | ||
noted in the description of the field | ||
--> | ||
|
||
**Optional** | ||
|
||
- `homeDir` (string) - The `$Home` directory in AMI image; default to `/home/ubuntu` | ||
|
||
<!-- | ||
A basic example on the usage of the provisioner. Multiple examples | ||
can be provided to highlight various configurations. | ||
|
||
--> | ||
|
||
### Example Usage | ||
|
||
```hcl | ||
packer { | ||
required_plugins { | ||
amazon = { | ||
version = ">= 0.0.2" | ||
source = "github.com/hashicorp/amazon" | ||
} | ||
} | ||
} | ||
|
||
source "amazon-ebs" "hashicorp-aws" { | ||
ami_name = "packer-plugin-hashicorp-aws-acc-test-ami" | ||
force_deregister = "true" | ||
force_delete_snapshot = "true" | ||
|
||
instance_type = "t2.micro" | ||
launch_block_device_mappings { | ||
device_name = "/dev/sda1" | ||
volume_size = 8 | ||
volume_type = "gp2" | ||
delete_on_termination = true | ||
} | ||
region = "us-west-1" | ||
source_ami_filter { | ||
filters = { | ||
name = "ubuntu/images/*ubuntu-*-22.04-amd64-server-*" | ||
root-device-type = "ebs" | ||
virtualization-type = "hvm" | ||
} | ||
most_recent = true | ||
owners = ["099720109477"] | ||
} | ||
ssh_username = "ubuntu" | ||
} | ||
|
||
build { | ||
sources = [ | ||
"source.amazon-ebs.hashicorp-aws" | ||
] | ||
|
||
provisioner "hashicorp-aws-webservice-provisioner" { | ||
homeDir = "/home/ubuntu" | ||
warSource = "my-webservice.war" | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) Jiaqi Liu | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
//go:generate packer-sdc mapstructure-to-hcl2 -type Config | ||
|
||
package webservice | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
basicProvisioner "github.com/QubitPi/packer-plugin-hashicorp-aws/provisioner/basic-provisioner" | ||
fileProvisioner "github.com/QubitPi/packer-plugin-hashicorp-aws/provisioner/file-provisioner" | ||
sslProvisioner "github.com/QubitPi/packer-plugin-hashicorp-aws/provisioner/ssl-provisioner" | ||
"github.com/hashicorp/hcl/v2/hcldec" | ||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer" | ||
"github.com/hashicorp/packer-plugin-sdk/template/config" | ||
"github.com/hashicorp/packer-plugin-sdk/template/interpolate" | ||
"path/filepath" | ||
) | ||
|
||
type Config struct { | ||
WarSource string `mapstructure:"warSource" required:"true"` | ||
HomeDir string `mapstructure:"homeDir" required:"false"` | ||
|
||
ctx interpolate.Context | ||
} | ||
|
||
type Provisioner struct { | ||
config Config | ||
} | ||
|
||
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { | ||
return p.config.FlatMapstructure().HCL2Spec() | ||
} | ||
|
||
func (p *Provisioner) Prepare(raws ...interface{}) error { | ||
err := config.Decode(&p.config, nil, raws...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, communicator packersdk.Communicator, generatedData map[string]interface{}) error { | ||
p.config.HomeDir = sslProvisioner.GetHomeDir(p.config.HomeDir) | ||
|
||
warFileDst := fmt.Sprintf(filepath.Join(p.config.HomeDir, "ROOT.war")) | ||
|
||
err := fileProvisioner.Provision(p.config.ctx, ui, communicator, p.config.WarSource, warFileDst) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return basicProvisioner.Provision(ctx, ui, communicator, getCommands(p.config.HomeDir)) | ||
} | ||
|
||
func getCommands(homeDir string) []string { | ||
return append( | ||
getCommandsUpdatingUbuntu(), | ||
append(getCommandsInstallingJDK17(), getCommandsInstallingJetty(homeDir)...)..., | ||
) | ||
} | ||
|
||
func getCommandsUpdatingUbuntu() []string { | ||
return []string{ | ||
"sudo apt update && sudo apt upgrade -y", | ||
"sudo apt install software-properties-common -y", | ||
} | ||
} | ||
|
||
// Install JDK 17 - https://www.rosehosting.com/blog/how-to-install-java-17-lts-on-ubuntu-20-04/ | ||
func getCommandsInstallingJDK17() []string { | ||
return []string{ | ||
"sudo apt update -y", | ||
"sudo apt install openjdk-17-jdk -y", | ||
"export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64", | ||
} | ||
} | ||
|
||
// Install and configure Jetty (for JDK 17) container | ||
func getCommandsInstallingJetty(homeDir string) []string { | ||
return []string{ | ||
"export JETTY_VERSION=11.0.15", | ||
"wget https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-home/$JETTY_VERSION/jetty-home-$JETTY_VERSION.tar.gz", | ||
"tar -xzvf jetty-home-$JETTY_VERSION.tar.gz", | ||
"rm jetty-home-$JETTY_VERSION.tar.gz", | ||
fmt.Sprintf("export JETTY_HOME=%s/jetty-home-$JETTY_VERSION", homeDir), | ||
"mkdir jetty-base", | ||
"cd jetty-base", | ||
"java -jar $JETTY_HOME/start.jar --add-module=annotations,server,http,deploy,servlet,webapp,resources,jsp", | ||
fmt.Sprintf("mv %s/ROOT.war webapps/ROOT.war", homeDir), | ||
"cd ../", | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.