che icon indicating copy to clipboard operation
che copied to clipboard

Setting Environment variables Using Factories

Open MuraliVidhya opened this issue 9 years ago • 17 comments

Using Factories, do i have an option to use variables/attributes as environment variables. For example - during Factory creation - i would like to have a variable (say ticketID) as an attribute/some parameter in factory configuration json and would use the same inside my workspace as environment variable. Is there a possibility to achieve this ?

Version: Eclipse Che 5.8.0 Docker 1.13.0

MuraliVidhya avatar Apr 27 '17 06:04 MuraliVidhya

@mvidhya86 currently, the only way to achieve what you are trying to achieve is to use a dockerfile rather than image in a recipe object. In your factory.json it will look like:

"recipe": {
       "type": "dockerfile",
       "content": "FROM eclipse/ubuntu_jdk8\nENV key value\n",
       "contentType": "text/x-dockerfile"
   }

ghost avatar Apr 27 '17 07:04 ghost

@eivantsov - thanks for your comments. In my case - these values are dynamic.
The case here is - if the Factory url has some additions like - http://servername:8080/dashboard/#/factory?id=1234 From the factory url - retrieve id and its value and use it like $key : $value in factory configuration (json) And this has to be visible inside my workspace (as env variable or by other means).

Does Che provide such options ?

MuraliVidhya avatar Apr 27 '17 10:04 MuraliVidhya

If you update your factory.json the way I proposed, you will have envs in a workspace. However, their values will be static.

ghost avatar Apr 27 '17 10:04 ghost

That means are there no means for passing arguments to the factory url which are used "as variables" in the factory json? In other words, can the same factory be used to create workspaces which are very similar one to another (ex: just working on a different jira ticket) ?

diogosaad avatar Apr 27 '17 14:04 diogosaad

No, there's no such functionality.

ghost avatar Apr 27 '17 14:04 ghost

What is the alternative? Create a workspace from teh factory and later use an API to set variables in the workspace? BTW, is there such a concept as "workspace variables"? or just the linux environment variables?

diogosaad avatar Apr 27 '17 15:04 diogosaad

All env vars that end up in a workspace container are added with Docker. But in a container, they are just like linux envvars

ghost avatar Apr 27 '17 16:04 ghost

Hi @mvidhya86 . You mentioned your use case around having a different factory per jira tickets. In this case, that means you would have a system, where you can define a "Factory template" and then depending on the jira ticket, you'll create a new factory based on that template. It would be when you create the factory that you replace the parameters from the template. As a result, you'll have a different factory per jira tickets.

It's exactly what Codenvy is providing with the JIRA plugin: https://codenvy.io/docs/integration-guide/issue-management/index.html

slemeur avatar Apr 27 '17 16:04 slemeur

But that would result in many factories. In the end I want to have one workspace per ticket. ...and somewhere in the workspace I need to store the ticket number so I can navigate back, create automatic commit messages, etc.

On Apr 27, 2017 6:11 PM, "Stévan Le Meur" [email protected] wrote:

Hi @mvidhya86 https://github.com/mvidhya86 . You mentioned your use case around having a different factory per jira tickets. In this case, that means you would have a system, where you can define a "Factory template" and then depending on the jira ticket, you'll create a new factory based on that template. It would be when you create the factory that you replace the parameters from the template. As a result, you'll have a different factory per jira tickets.

It's exactly what Codenvy is providing with the JIRA plugin: https://codenvy.io/docs/integration-guide/issue-management/index.html

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eclipse/che/issues/4935#issuecomment-297763230, or mute the thread https://github.com/notifications/unsubscribe-auth/AC0NeBjSVTFijkDCLK5Qtu1bBgRokDtVks5r0L47gaJpZM4NJxee .

diogosaad avatar Apr 27 '17 16:04 diogosaad

I think you may store this info in machine attributes too:

          "dev-machine": {
            "attributes": {
              "memoryLimitBytes": "2147483648",
              "new": "smth"
            }
        }

I am not sure this solve your problem though.

ghost avatar Apr 27 '17 16:04 ghost

@eivantsov - If we store the variable in machine attributes - how do i retrieve inside my workspace ?

MuraliVidhya avatar May 04 '17 03:05 MuraliVidhya

@mvidhya86 you may use curl in a terminal, for example. Can you elaborate on how exactly you need to get those?

ghost avatar May 04 '17 10:05 ghost

Yes, considering that i updated the workspace configuration with new attribute dev-machine": { "attributes": { "memoryLimitBytes": "2147483648", "tempid": "test" },

After opening the workspace - if i create a new command (command line - specify syntax) to retrieve this custom attribute value, what should i use in curl.

MuraliVidhya avatar May 09 '17 05:05 MuraliVidhya

Smth like this should work for you:

curl -s http://$IP:8080/api/workspace/$ws-id | jq .attributes | grep tempid | awk '{print $2}' | sed 's/\"//g'

However, you may need jq installed in your stack if you are going to call this API from within a workspace. And replace $ws-id with an actual workspace ID.

ghost avatar May 10 '17 05:05 ghost

@mvidhya86 did my suggestion work for you?

ghost avatar May 15 '17 10:05 ghost

@eivantsov not related directly to this issue but just wanted to say thanks as your suggestion of using attributes in combination with curl/jq saved us a lot of manual work #4800

davidwindell avatar May 17 '17 16:05 davidwindell

@davidwindell good to know. @mvidhya86 i am closing the issue. Feel free to reopen if you have further questions.

ghost avatar May 18 '17 06:05 ghost