Terraform - GCloud Container Registry Sample - docker

I'm working with terraforming gcloud resources and need to create gcloud container registry and trying to use below sample from terraform.io
data "google_container_registry_repository" {}
output "gcr_location" {
value = "${data.google_container_registry_repository.repository_url}"
}
and receving below error when I run terraform plan
'data' must be followed by exactly two strings: a type and a name
Any working sample that I can refer to ?
terraform.io syntax : https://www.terraform.io/docs/providers/google/d/google_container_registry_repository.html
terraform version:
Terraform v0.11.2
Edit : updated to Terraform v0.11.3 and still same problem.

Try this:
data "google_container_registry_repository" "myregistry" {}
output "gcr_location" {
value = "${data.google_container_registry_repository.myregistry.repository_url}"
}

Related

How to get the docker host name in azure itoedge enviroment variable

Hi I am trying to get the hostname into my azure module by getting it form the enviroment variables. the module is written in C# and .NetCore 3.1
var deviceId = Environment.GetEnvironmentVariable("HOST_HOSTNAME");
I have tried to add the variable in de deployment template
"createOptions": {
"Cmd": [
"-e HOST_HOSTNAME=(hostname)"
]
}
The result is
deviceId == null
Can you try using "env" in your deployment template? You should add it on the same level as "settings" JSON object. Something like:
"env": {
"HOS_HOSTNAME": {
"value": "<valuehere>"
}
}
You can also do this in the Azure Portal. See for example how is done in the tutorial Give modules access to a device's local storage

Issue while reading openshift template from repository(bitbucket)

I have a jenkins pipeline script to create an app using openshift template placed in a repository(Bit Bucket). However, i get the below mentioned error everytime.
Note: I have configured credentials in Jenkins as well.
Version
openshift v3.11.200
kubernetes v1.11.0+d4cacc0
Steps To Reproduce
PipeLine Script:
// path of the template to use
def templatePath = 'https://xxxx/git/users/pdeb/repos/mashery-local/raw/acs/tml-acs-template.json'
// name of the template that will be created
def templateName = 'mashery-local'
// NOTE, the "pipeline" directive/closure from the declarative pipeline syntax needs to include, or be nested outside,
// and "openshift" directive/closure from the OpenShift Client Plugin for Jenkins. Otherwise, the declarative pipeline engine
// will not be fully engaged.
pipeline {
agent any
stages {
stage('preamble') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
echo "Using project: ${openshift.project()}"
}
}
}
}
}
stage('cleanup') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
// delete everything with this template label
openshift.selector("all", [ template : templateName ]).delete()
// delete any secrets with this template label
if (openshift.selector("secrets", templateName).exists()) {
openshift.selector("secrets", templateName).delete()
}
}
}
} // script
} // steps
} // stage
stage('create') {
steps {
script {
openshift.withCluster() {
openshift.withProject() {
// create a new application from the templatePath
openshift.newApp(templatePath)
}
}
} // script
} // steps
} // stage
} // stages
} // pipeline
Current Result:
*ERROR: new-app returned an error;
{reference={}, err=error: unable to load template file "https://rndwww.nce.amadeus.net/git/users/pdeb/repos/mashery-local-on-acs/raw/acs/tml-acs-template.json": unable to decode "https://rndwww.nce.amadeus.net/git/users/pdeb/repos/mashery-local-on-acs/raw/acs/tml-acs-template.json": couldn't get version/kind; json parse error: json: cannot unmarshal string into Go value of type struct { APIVersion string "json:\"apiVersion,omitempty\""; Kind string "json:\"kind,omitempty\"" }
error: git ls-remote failed with: fatal: https://rndwww.nce.amadeus.net/git/users/pdeb/repos/mashery-local-on-acs/raw/acs/tml-acs-template.json/info/refs not valid: is this a git repository?; local file access failed with: stat https://rndwww.nce.amadeus.net/git/users/pdeb/repos/mashery-local-on-acs/raw/acs/tml-acs-template.json: no such file or directory
error: unable to locate any images in image streams, templates loaded in accessible projects, template files, local docker images with name "https://rndwww.nce.amadeus.net/git/users/pdeb/repos/mashery-local-on-acs/raw/acs/tml-acs-template.json"
Argument 'https://rndwww.nce.amadeus.net/git/users/pdeb/repos/mashery-local-on-acs/raw/acs/tml-acs-template.json' was classified as an image, image~source, or loaded template reference.
The 'oc new-app' command will match arguments to the following types:
1. Images tagged into image streams in the current project or the 'openshift' project
- if you don't specify a tag, we'll add ':latest'
2. Images in the Docker Hub, on remote registries, or on the local Docker engine
3. Templates in the current project or the 'openshift' project
4. Git repository URLs or local paths that point to Git repositories
--allow-missing-images can be used to point to an image that does not exist yet.
See 'oc new-app -h' for examples., verb=new-app, cmd=oc --server=https://10.224.0.1:443 --certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt --namespace=lhmdw-tml-dev --token=XXXXX new-app https://rndwww.nce.amadeus.net/git/users/pdeb/repos/mashery-local-on-acs/raw/acs/tml-acs-template.json -o=json , out=, status=1}
Finished: FAILURE*
Expected Result:
To run the template successfully.
The following error message indicates that your template file has an incorrect format:
json: cannot unmarshal string into Go value of type struct { APIVersion string "json:\"apiVersion,omitempty\""; Kind string "json:\"kind,omitempty\"" }
So you should fix the content of the template tml-acs-template.json, as currently this file cannot be used.

Can you run Docker containers in GCP via Terraform?

I have created a Docker image that I'd like to run in GCP using Terraform. I have tagged and pushed the image to GCR like this:
docker tag carlspring/hello-spring-boot:1.0 eu.gcr.io/${PROJECT_ID}/carlspring/hello-spring-boot:1.0
docker push eu.gcr.io/carlspring/carlspring/hello-spring-boot:1.0
I have the following code:
provider "google" {
// Set this to CREDENTIALS
credentials = file("credentials.json")
// Set this to PROJECT_ID
project = "carlspring"
region = "europe-west2"
zone = "europe-west2-a"
}
resource "google_compute_network" "vpc_network" {
name = "carlspring-terraform-network"
}
resource "google_compute_instance" "docker" {
count = 1
name = "tf-docker-${count.index}"
machine_type = "f1-micro"
zone = var.zone
tags = ["docker-node"]
boot_disk {
initialize_params {
image = "carlspring/hello-spring-boot"
}
}
}
After doing:
terraform init
terraform plan
terraform apply
I get:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
google_compute_instance.docker[0]: Creating...
Error: Error resolving image name 'carlspring/hello-spring-boot': Could not find image or family carlspring/hello-spring-boot
on main.tf line 18, in resource "google_compute_instance" "docker":
18: resource "google_compute_instance" "docker" {
The examples I've seen online are either using K8s, or starting a VM image running a Linux in which Docker is installed and an image is being started. Can't I just simply use my own container to start the instance?
google_compute_instance expects a VM image, not a Docker image. If you want to deploy Docker images to GCP, the easiest option is Cloud Run. To use it with Terraform you need cloud_run_service.
For example:
resource "google_cloud_run_service" "default" {
name = "cloudrun-srv"
location = "us-central1"
template {
spec {
containers {
image = "eu.gcr.io/carlspring/carlspring/hello-spring-boot:1.0"
}
}
}
traffic {
percent = 100
latest_revision = true
}
}
Note that I used eu.gcr.io/carlspring/carlspring/hello-spring-boot:1.0 and not carlspring/hello-spring-boot. You must use the fully qualified name as the short one points to Docker Hub where your image will not be found.
Terraform can be used to create a GCP VM Instance with Docker Image.
Here is an example: https://github.com/terraform-providers/terraform-provider-google/issues/1022#issuecomment-475383003
Hope this helps.
The following line indicates the image does not exist:
Error: Error resolving image name 'carlspring/hello-spring-boot': Could not find image or family carlspring/hello-spring-boot
You should tag the image as eu.gcr.io/carlspring/hello-spring-boot:1.0.
Or alternatively, change image reference in boot_disk block to be eu.gcr.io/carlspring/carlspring/hello-spring-boot:1.0.
You can do this using a VM in GCE whose operating system is based on a Google-supplied Container OS image. You then can use this terraform module that facilitates the fetching and running of a container image.

Use Docker Pipeline Plugin without interactive mode

I'm trying to use docker with Jenkins Scripted pipeline, and faced with several problems.
If I use it in sh docker ... it results in an error
command not found docker
I tried to fix it by changing Install setting in Global Configuration tool - but not succeed with it.
I'm trying to use Docker plugin now.
def run_my_stage(String name, String cmd, String commit) {
return {
stage(name) {
node("builder") {
docker.withRegistry("192.168.1.33:5000") {
def myimg = docker.image("my-img")
sh "docker pull ${myimg.imageName()}"
sh "docker run ${cmd}"
}
}
}
}
Where cmd == --user=\$UID --rm -t -v ./build/:/home/user/build 192.168.1.33:5000/my-img
I use this code for parallel stages (list of stages generated dynamically), and got this error
java.net.MalformedURLException: no protocol: 192.168.1.33:5000
What is proper usage of this plugin?
I found a lot of examples with withRun and other methods from docker, but I don't need to run any commands inside this image, I have command in Dockerfile (so it built-in for my container).
The error itself has the answer :).
java.net.MalformedURLException: no protocol: 192.168.1.33:5000
You are missing protocol in custom registry. Refer https://jenkins.io/doc/book/pipeline/docker/#custom-registry
def run_my_stage(String name, String cmd, String commit) {
return {
stage(name) {
node("builder") {
docker.withRegistry("https://192.168.1.33:5000") {
def myimg = docker.image("my-img")
sh "docker pull ${myimg.imageName()}"
sh "docker run ${cmd}"
}
}
}
}
You are missing the protocol, the registry must be https://192.168.1.33:5000
Also I have problem with relative path, but simple fix with adding pwd before relative path to build fixed.
Thx #yzT

Jenkins doesn't fully resolve variables values

I have a Jenkinsfile where I want to declare some variables to later use in other sections. I know about Jenkins use the Groovy syntax rules for variable expansion but I expected some variable values where fully resolved. Instead I found that they just resolved in one level. Code inlcuded below shows what I want: Trying to use $PATHTOFILES variable to link data downloaded/generated in previous stages for a Docker container. Instead the variable is just resolved as pwd.
I tried several options seeking to expand the values that I need, so far unsuccessfully.
agent none
environment {
...
PATHTOFILES = "`pwd`"
...
}
...
stage('Unit'){
agent {
docker {
image "${DOCKERIMAGEURI}"
args '-v $PATHTOFILES:$CONTAINER_DATA_PATH'
}
}
}
Logs show that a value '-v `pwd`:/some/valid/path' is used as argument, leading to an error. Any ideas?
You did wrong to get current workspace of job. One of below 3 methods should work:
1) use shell cmd: pwd and pipeline step: sh() together
PATHTOFILES = sh(script: 'pwd', returnStdout:true).trim()
2) use pipeline step: pwd()
PATHTOFILES = pwd()
3) use environment variable: WORKSPACE
PATHTOFILES = env.WORKSPACE

Resources