Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle to manage docker images #27

Open
neilhwatson opened this issue Nov 14, 2016 · 1 comment
Open

Bundle to manage docker images #27

neilhwatson opened this issue Nov 14, 2016 · 1 comment

Comments

@neilhwatson
Copy link
Owner

neilhwatson commented Nov 14, 2016

Keep docker images current by checking existing images and build new ones from Dockerfiles if current ones are old or missing.

Getting info from Docker API

  1. Get creation time of image with given RepoTag:
    curl -s --unix-socket /run/docker.sock http://docker/images/json|jq '.[] | select( .RepoTags[] == "my_tag:latest" )|.Created''

Compare time against Dockerfile and support files' ctime. Invoke new build if Created is older.

@neilhwatson
Copy link
Owner Author

Prototype

Assumptions

  1. Dockerfile and other support files have been staged by another bundle.
  2. We pole the agent host's Docker api via socket, not remote.
  3. Docker service is installed and running on agent host.

Code

body common control
{
    bundlesequence => { "main", };
}

bundle agent main
{
    methods:

        "any" usebundle => docker_images;
}

bundle agent docker_images 
{
   vars:
      "images_wanted"
         comment => "Sample data, would normally be a passed in param file",
         data    => parsejson( '
            [
               {
                  docker_dir: "/home/neil/src/neil/cfbot"
                  Tag:         "cfbot",
                  RepoTags:    "cfbot:latest",
               }
            ]'
         );

      "i" slist => getindices( "images_wanted" );

   methods:

      "${i}"
         comment   => "Pass do another bunle for simpler iteration",
         usebundle => promise_docker_image( @{images_wanted}, ${i} );

}

bundle agent promise_docker_image ( images_wanted, i )
{
   vars:

      "image"
         comment => "Get image info from docker api for the given tag",
         data    => parsejson(
            execresult( 

            "/usr/bin/curl --silent \
               --unix-socket /run/docker.sock http://docker/images/json \
               | jq '.[] | select( .RepoTags[] \
               == \"${images_wanted[${i}][RepoTags]}\" )|.'",

            "useshell" )
            );

      "docker_files"
         comment => "Get list of given docker files",
         slist   =>
            splitstring(
               execresult(
                  "/usr/bin/find ${images_wanted[${i}][docker_dir]} -type f"
                  , "noshell"
               ),
               "\n", "999"
            );

   classes:
      "build_docker_image_${i}_${images_wanted[${i}][RepoTags]}"
         comment    => "Test if any related docker file is newer",
         expression =>
            isgreaterthan(
               filestat( "${docker_files}", mtime ), ${image[Created]}
            );

   reports:
      "docker build -t ${images_wanted[${i}][Tag]} ."
         comment => "Here we would force a rebuild of the docker image",
         # contain => usedir( "${images_wnated[${i}][docker_dir]}" ),
         if      => canonify( 
                       "build_docker_image_${i}_${images_wanted[${i}][RepoTags]}"
                    ); 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant