Skip to content

Commit

Permalink
add jfrog integration module
Browse files Browse the repository at this point in the history
  • Loading branch information
matifali committed Sep 26, 2023
1 parent 34aedce commit cdb2ecc
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .sample/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ resource "coder_script" "MODULE_NAME" {
LOG_PATH : var.log_path,
})
run_on_start = true
run_on_stopt = false
run_on_stop = false
}

resource "coder_app" "MODULE_NAME" {
Expand Down
56 changes: 55 additions & 1 deletion jfrog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,58 @@ tags: [integration]

# JFrog

TODO
<!-- Describes what this module does -->

<!-- Add a screencast or screenshot here put them in .images directory -->

```hcl
module "jfrog" {
source = "https://registry.coder.com/modules/jfrog"
}
```

## Examples

### Example 1

Install the Dracula theme from [OpenVSX](https://open-vsx.org/):

```hcl
module "jfrog" {
source = "https://registry.coder.com/modules/jfrog"
agent_id = coder_agent.example.id
extensions = [
"dracula-theme.theme-dracula"
]
}
```

Enter the `<author>.<name>` into the extensions array and code-server will automatically install on start.

### Example 2

Configure VS Code's [settings.json](https://code.visualstudio.com/docs/getstarted/settings#_settingsjson) file:

```hcl
module "jfrog" {
source = "https://registry.coder.com/modules/jfrog"
agent_id = coder_agent.example.id
extensions = [ "dracula-theme.theme-dracula" ]
settings = {
"workbench.colorTheme" = "Dracula"
}
}
```

### Example 3

Run code-server in the background, don't fetch it from GitHub:

```hcl
module "jfrog" {
source = "https://registry.coder.com/modules/jfrog"
agent_id = coder_agent.example.id
offline = true
}
```
111 changes: 111 additions & 0 deletions jfrog/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
terraform {
required_version = ">= 1.0"

required_providers {
coder = {
source = "coder/coder"
version = ">= 0.12"
}
artifactory = {
source = "registry.terraform.io/jfrog/artifactory"
version = "~> 8.4.0"
}
}
}

variable "jfrog_host" {
type = string
description = "JFrog instance hostname. e.g. YYY.jfrog.io"
}

variable "artifactory_access_token" {
type = string
description = "The admin-level access token to use for JFrog."
}

# Configure the Artifactory provider
provider "artifactory" {
url = "https://${var.jfrog_host}/artifactory"
access_token = var.artifactory_access_token
}

variable "agent_id" {
type = string
description = "The ID of a Coder agent."
}

variable "package_managers" {
type = map(string)
description = <<EOF
A map of package manager names to their respective artifactory repositories.
For example:
{
"npm": "npm-local",
"go": "go-local",
"pypi": "pypi-local"
}
EOF
}

# Configure the Artifactory provider
provider "artifactory" {
url = "https://${var.jfrog_host}/artifactory"
access_token = var.artifactory_access_token
}

resource "artifactory_scoped_token" "me" {
# This is hacky, but on terraform plan the data source gives empty strings,
# which fails validation.
username = length(local.artifactory_username) > 0 ? local.artifactory_username : "plan"
}

resource "coder_script" "jfrog" {
agent_id = var.agent_id
display_name = "jfrog"
icon = local.icon_url
script = templatefile("${path.module}/run.sh", {
JFROG_HOST : var.jfrog_host,
ARTIFACTORY_USERNAME : artifactory_scoped_token.me.username,
ARTIFACTORY_ACCESS_TOKEN : artifactory_scoped_token.me.token,
REPOSITORY_NPM : lookup(var.package_managers, "npm", ""),
REPOSITORY_GO : lookup(var.package_managers, "go", ""),
REPOSITORY_PYPI : lookup(var.package_managers, "pypi", ""),
})
run_on_start = true
}

resource "coder_app" "jfrog" {
agent_id = var.agent_id
slug = "jfrog"
display_name = "jfrog"
url = "http://localhost:${var.port}"
icon = loocal.icon_url
subdomain = false
share = "owner"

# Remove if the app does not have a healthcheck endpoint
healthcheck {
url = "http://localhost:${var.port}/healthz"
interval = 5
threshold = 6
}
}

data "coder_parameter" "jfrog" {
type = "list(string)"
name = "jfrog"
display_name = "jfrog"
icon = local.icon_url
mutable = var.mutable
default = local.options["Option 1"]["value"]

dynamic "option" {
for_each = local.options
content {
icon = option.value.icon
name = option.value.name
value = option.value.value
}
}
}

46 changes: 46 additions & 0 deletions jfrog/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env sh

BOLD='\033[0;1m'
echo "$${BOLD}Installing JFrog CLI..."

# The jf CLI checks $CI when determining whether to use interactive
# flows.
export CI=true

jf c rm 0 || true
echo ${ARTIFACTORY_ACCESS_TOKEN} | jf c add --access-token-stdin --url https://${JFROG_HOST} 0

# Configure the `npm` CLI to use the Artifactory "npm" repository.
if [ -z "${REPOSITORY_NPM}" ]; then
echo "🤔 REPOSITORY_NPM is not set, skipping npm configuration."
else
echo "📦 Configuring npm..."
jf rt npm-config set registry https://${JFROG_HOST}/artifactory/api/npm/${REPOSITORY_NPM}
cat << EOF > ~/.npmrc
email = ${ARTIFACTORY_EMAIL}
registry = https://${JFROG_HOST}/artifactory/api/npm/${REPOSITORY_NPM}
EOF
jf rt curl /api/npm/auth >> .npmrc
fi
# Configure the `pip` to use the Artifactory "python" repository.
if [ -z "${REPOSITORY_PYPI}" ]; then
echo "🤔 REPOSITORY_PYPI is not set, skipping pip configuration."
else
echo "🐍 Configuring pip..."
mkdir -p ~/.pip
cat << EOF > ~/.pip/pip.conf
[global]
index-url = https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}@${JFROG_HOST}/artifactory/api/pypi/${REPOSITORY_PYPI}/simple
EOF
fi
# Set GOPROXY to use the Artifactory "go" repository.
if [ -z "${REPOSITORY_GO}" ]; then
echo "🤔 REPOSITORY_GO is not set, skipping go configuration."
else
echo "🐹 Configuring go..."
export GOPROXY="https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}@${JFROG_HOST}/artifactory/api/go/${REPOSITORY_GO}"
fi
echo "🥳 Configuration comlete!"

0 comments on commit cdb2ecc

Please sign in to comment.