From ccf654ce8aeb9d783617cb90476119bae82063fb Mon Sep 17 00:00:00 2001 From: sbyrd Date: Tue, 6 Oct 2020 02:11:36 -0400 Subject: [PATCH] initial --- image.php | 31 ++++++ imageAnnotations.php | 17 +++ imageConfig.php | 53 ++++++++++ imageConfigConfig.php | 63 +++++++++++ podmanImageManager.php | 230 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 394 insertions(+) create mode 100644 image.php create mode 100644 imageAnnotations.php create mode 100644 imageConfig.php create mode 100644 imageConfigConfig.php create mode 100644 podmanImageManager.php diff --git a/image.php b/image.php new file mode 100644 index 0000000..276a74d --- /dev/null +++ b/image.php @@ -0,0 +1,31 @@ +imageName = (isset($vars['image']) ? $vars['image'] : + (isset($vars['name']) ? $vars['name'] : + (iiset($vars['imageName'] ? $vars['imageName'] : "" ) ) ) ) ; + if (isset( $vars['imageBasic']->annotations) ) + $annotations = $vars['imageBasic']->annotations; + elseif (isset($vars['annotations'])) + $annotations = $vars['annotations']; + + $this->annotations = new imageAnnotations($annotations); + if (isset($vars['imageConfig'])) + $this->config = new imageConfig($vars['imageConfig']); + } +} \ No newline at end of file diff --git a/imageAnnotations.php b/imageAnnotations.php new file mode 100644 index 0000000..9676f4e --- /dev/null +++ b/imageAnnotations.php @@ -0,0 +1,17 @@ + 2020-04-23T00:50:14.871449584Z + [author] => Cooini, LLC + [architecture] => amd64 + [os] => linux + + + [rootfs] => stdClass Object + ( + [type] => layers + [diff_ids] => Array + ( + [0] => sha256:7402c41ceaacfdd10fcdc3c64e67ed61797dadad8adaf081e9e9bea71e0eafc2 + ) + + ) + + [history] => Array + ( + [0] => stdClass Object + ( + [created] => 2020-04-23T00:50:14.871449584Z + [created_by] => /bin/sh + [author] => Cooini, LLC + ) + + ) + */ \ No newline at end of file diff --git a/imageConfigConfig.php b/imageConfigConfig.php new file mode 100644 index 0000000..769f0fb --- /dev/null +++ b/imageConfigConfig.php @@ -0,0 +1,63 @@ + stdClass Object + ( + [ExposedPorts] => stdClass Object + ( + [443/tcp] => stdClass Object + ( + ) + + [80/tcp] => stdClass Object + ( + ) + + ) + + [Env] => Array + ( + [0] => domain=example.com + [1] => domainAliases=example.com-iniapp.generic.host + ) + + [Entrypoint] => Array + ( + [0] => /bin/sh + [1] => -c + [2] => /usr/local/hosting/container/start.sh + ) + + [Cmd] => Array + ( + [0] => /usr/local/hosting/container/start.sh + ) + + [Volumes] => stdClass Object + ( + [/var/www/vhosts/example.com] => stdClass Object + ( + ) + + ) + + [Labels] => stdClass Object + ( + [io.buildah.version] => 1.11.6 + ) + + ) + */ \ No newline at end of file diff --git a/podmanImageManager.php b/podmanImageManager.php new file mode 100644 index 0000000..cb9f96c --- /dev/null +++ b/podmanImageManager.php @@ -0,0 +1,230 @@ +
$TheBody"); + } + + + return false; + } + + /** + * @param string $ImageName image name including repo address + */ + public function getImage(string $ImageNameComplete, $imageTag='latest') + { + $RepoURL = "https://".$ImageNameComplete; + if (preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/", $RepoURL) ) { + //Good URL, now lets work it + $URLbits = parse_url($RepoURL); + if ($URLbits) + { + $imageName = ltrim($URLbits['path'], "/"); + $registry = $URLbits['host'] .":".$URLbits['port']; + } + } + $tagsURL = "https://".$registry. "/v2/".$imageName."/tags/list"; + try { + $TagsRaw = $this->doRequest($tagsURL, null, null, true); + } + catch (\Exception $e) + { + throw new \Exception("Exception getting tags:".$e->getMessage()); + } + + if ($TagsRaw and isset($TagsRaw->tags)) + { + //echo "
tags
".print_r($TagsRaw,true)."
"; + + $ImagesReturn = array(); + foreach($TagsRaw->tags as $tag) + { + $ImageManifestURL = "https://".$registry."/v2/".$imageName."/manifests/".$tag; + // echo $ImageManifestURL; + try { + $ImageRaw = $this->doRequest($ImageManifestURL, null, array('Accept: application/vnd.oci.image.manifest.v1+json'),true); + // echo "
image maniest
".print_r($ImageRaw,true)."
"; + } + catch (\Exception $e) + { + throw new \Exception("Exception getting image manifest:".$e->getMessage()); + } + if (isset($ImageRaw->config->digest)) + { + $ImageConfigURL = "https://".$registry."/v2/".$imageName."/blobs/".$ImageRaw->config->digest; + // echo $ImageConfigURL; + $ImageConfigRaw = $this->doRequest($ImageConfigURL, null, null,true ); + //echo "
image config
".print_r($ImageConfigRaw,true)."
"; + + try { + $Image = new image( array('name'=>$ImageNameComplete, 'imageBasic'=>$ImageRaw, 'imageConfig'=>$ImageConfigRaw)); + if ($Image) + { + if ($imageTag === $tag ) + return $Image; + } + else + throw new \Exception("Image object failed to create while fetching"); + } + catch (\Exception $e) + { + throw new \Exception("Exception creating image object:".$e->getMessage()); + } + //echo "
image
".print_r($Image,true)."
"; + } + else + throw new \Exception("Could not find image config digest"); + } // tags loop + } // if tags exist + else + throw new \Exception("Could not get tags for image"); + + return false; + + } + + + /** + * @param string $repo + * @return image[] + * @throws \Exception + */ + public function getImages(string $repo ) + { + //podman.registry.generic.host:5000/httpdphp7 + $cache = $this->getCache($repo); + if ($cache) + { + return $cache; + } + + // Not cached, continue ; + + $ImagesReturn = array(); + $URL = 'https://'.$repo.'/v2/_catalog'; + if (preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/", $URL) ) + { + $Images = $this->doRequest( $URL, null, null, true ); + if (isset($Images->repositories) ) + { + foreach($Images->repositories as $imageName) + { + $Image = $this->getImage($repo ."/". $imageName); + if ($Image) + $ImagesReturn[$imageName] = $Image; + + } //Images loop + } + else + throw new \Exception("Images not valid"); + } + else + { + throw new \Exception("Repo not valid"); + } + + $cache = $this->setCache($repo, $ImagesReturn); + return $ImagesReturn; + } + + private function getCache($name) + { + $CacheDIr = __DIR__ . "/cache"; + $name = str_replace(array('.',":"),'-',$name); + if (file_exists( $CacheDIr . "/".$name ) and is_readable($CacheDIr . "/".$name)) + { + if ( (mktime() - filectime($CacheDIr . "/".$name) <= 60) ) + { + $data = file_get_contents($CacheDIr . "/".$name); + return unserialize( $data ); + } + else + { + return false; + } + } + return false; + } + + private function setCache($name, $Object) + { + $CacheDIr = __DIR__ . "/cache"; + if (! file_exists($CacheDIr)) + mkdir($CacheDIr); + if (file_exists($CacheDIr)) + { + $name = str_replace(array('.',":"),'-',$name); + return file_put_contents( $CacheDIr . "/".$name, serialize( $Object ) ); + } + else + throw new \Exception("Image cache directory does not exist and could not create"); + } +} \ No newline at end of file