詳細検索

Yes, let's move to HTTP/2 (hands-on)

Avatar
by 吉田将之
6 min read

Yes, let's move to HTTP/2 (hands-on)
Translated from 日本語 • View original

Hello, I'm Yoshida, an engineer.

Last time, I wrote an article about migrating to HTTP/2 (OpsWorks), and this time it will be a sequel. (I'm sorry for the time that has passed since last time)

In the previous article, we talked about building a simple environment on the Chef12 stack using AWS OpsWorks to migrate to HTTP/2. However, in reality, the Chef12 stack is different from the stack before Chef11, and the way the recipes were worked up to that point must be changed. This is a hands-on edition that focuses on solving problems in the OpsWorks Chef12 stack in the field.

Practical

Part 1. Run Community Cookbook-powered recipes on OpsWorks' Chef 12 stack

The most important thing to note in the Chef12 stack is that the community cookbook references you use when running recipes are no longer automatic. When I tried to run a recipe using the community cookbook as it was, I got a message saying, "Community cookbook? I can't find that!" I get angry.

So as a solution, create a cookbook archive containing the community cookbook in advance and reference the location where you uploaded it to S3.

Create a cookbook archive with community cookbooks

Prerequisites You must have BerkShelf installed to create an archive. If you don't have one, install ChefDK or install BerkShelf with a gem. ChefDK: https://downloads.chef.io/chefdk Gem: gem install berkshelf

[Procedure] 1. Navigate to the cookbook directory locally

  1. Run the 'berks package' command to create an archive This will result in an archive with a timestamp such as cookbooks-1506749951.tar.gz by default. If you want to specify a file name, run the command 'berks package {filename}.tar.gz'.

  2. Create an S3 bucket to store the archive

  3. Upload the archive to the bucket you created in step 3

The AWS CLI's S3 command is useful. 'aws s3 cp cookbooks.tar.gz s3://{bucket name}'

After running, make sure that the archive file is uploaded to S3.

  1. Configure the OpsWorks stack to reference archive files

In the AWS console, select the archive file you just created and copy the link URL in the dialog that appears.

Go to the OpsWorks stack settings edit page and configure the following settings: ・Use custom Chef cookbooks: ON ・Repository type: S3 Archive ・Repository URL: コピーしたアーカイブファイルリンクURLを貼り付け

That's it, you can run recipes using the community cookbook.

Part 2. Automate cookbook archive creation & upload to S3

In Part 1, we can now use the Community Cookbook in OpsWorks.

However, it is too troublesome to create a cookbook archive and upload it to S3 every time you modify a recipe. Furthermore, if you have a Git repository that stores cookbooks in the field, it is desirable to create branches according to each environment such as master, stage, and develop, and store the cookbook archive in S3 for each of those branches.

These tasks are automated.

Automation Procedure

To automate it, we will use a CI tool called Travis CI. Travis: I think it's possible if it's a CI tool that can be linked to Github even if it's not a CI, so please try it.

Suppose that the Git repository of the cookbook is also branched and developed according to the Git flow. Also, refer to the cookbooks of each branch, such as master, stage, and develop, in each environment (stack).

For example, the master branch has a cookbook archive called master.tar.gz in S3, which is referenced in the stack for production. If it is a stage branch, the cookbook archive called stage.tar.gz is referenced in the stack for the verification environment.

Therefore, the goal of automation is to "automatically create a cookbook archive and upload it to S3 when the source is merged into a specified branch" is.

Create a Travis CI Configuration File (.travis.yml)

To use Travis CI, you first need to create a configuration file called .travis.yml. Place the following .travis.yml files in the root directory of your cookbook repository. Just copy and paste it as it is.

I will explain what kind of settings you have in order.

Branch Designation

The first branches~ part is a description that specifies which branch to run Travis on when it is merged. Here we specify the master, stage, and develop branches. By specifying this, you will not have to create an extra archive, so it is recommended to write it as much as possible.

S3 Settings

Set up Travis to upload files to S3. The official documentation is here. Uploading Artifacts on Travis CI

Specify the region of the S3 bucket to upload to. If the bucket is in the Tokyo region, change the specification to "ap-northeast-1", if not.

Specify the file path of the cookbook archive from which you want to upload it. Here, the file name is named after the branch for ease of understanding, so it is "$TRAVIS_BRANCH.tar.gz". *$TRAVIS_BRANCH is a variable that points to the target branch name in Travis.

Specify the file path in the S3 bucket to be uploaded to. Note that if you do not specify target_paths, it will be placed in a fairly deep hierarchy by default, which also includes the directory of the Git repository name. Specify "./" to simply fit directly under the bucket.

Execution Command Settings

Finally, set the command to run in Travis, that is, the process until the cookbook archive is created.

To create an archive, you must install BerkShelf as mentioned above. After installing Ruby first, drop BerkShelf with the gem.

Generate an archive with a 'berks package' to finish.

Travis CI Execution Repository Settings

Of course, just pushing the .travis.yml to Git won't run Travis. In the Travis CI dashboard, you will need to configure various settings in the cookbook repository.

First, to go to the repository settings screen, specify the cookbook repository on the management screen. Next, press the "More options" button displayed in the upper right corner of the screen as shown below, and various menus will appear, so click "Settings" at the top.

Travis Run Timing Settings

At the top of the settings screen, you can set it like this.

It is especially important to turn on Build branch updates and turn off Build pull request updates. Build branch updates is whether or not Travis runs when the branch in question is updated. For example, if you create a pull request from the feature branch to the develop branch and then leave this setting on when you merge, the cookbook archive in the develop branch will not be uploaded to S3.

Build pull request updates is whether or not to run Travis when a GitHub pull request is updated. If this is turned on, I turn it off because every time I push a fix to the merge source branch before merging a pull request, Travis will run in vain.

Configure the credentials required for S3 uploads

Finally, set the required credentials for S3 upload.

You will need three things: ARTIFACTS_KEY=(AWS access key id) ARTIFACTS_SECRET=(AWS secret access key) ARTIFACTS_BUCKET=(S3 bucket name).

Please refer to this as it is also described in the official documentation. Uploading Artifacts on Travis CI

That's it, Travis CI can now automatically create cookbooks that you can reference in your OpsWorks stack!

In addition to the previous article, you can see that migrating to HTTP/2 for projects using OpsWorks requires these steps.

Have a good HTTP/2 life!

Related Articles