Add Slackme module
							parent
							
								
									216d32f3aa
								
							
						
					
					
						commit
						d02ca12d42
					
				@ -0,0 +1,40 @@
 | 
			
		||||
terraform {
 | 
			
		||||
  required_version = ">= 1.0"
 | 
			
		||||
 | 
			
		||||
  required_providers {
 | 
			
		||||
    coder = {
 | 
			
		||||
      source  = "coder/coder"
 | 
			
		||||
      version = ">= 0.12"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
variable "agent_id" {
 | 
			
		||||
  type        = string
 | 
			
		||||
  description = "The ID of a Coder agent."
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
variable "external_provider_id" {
 | 
			
		||||
    type        = string
 | 
			
		||||
    description = "The ID of an external provider."
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
data "coder_external_auth" "slack" {
 | 
			
		||||
  id = var.external_provider_id
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "coder_script" "install_slackme" {
 | 
			
		||||
    agent_id = var.agent_id
 | 
			
		||||
    display_name     = "install_slackme"
 | 
			
		||||
    script = <<EOF
 | 
			
		||||
    #!/usr/bin/env bash
 | 
			
		||||
 | 
			
		||||
    CODER_DIR=$(dirname $(which coder))
 | 
			
		||||
    cat > $CODER_DIR/slackme <<EOF
 | 
			
		||||
    ${templatefile("${path.module}/slackme.sh", {
 | 
			
		||||
        PROVIDER_ID: data.coder_external_auth.slack.id
 | 
			
		||||
    })}
 | 
			
		||||
    \EOF
 | 
			
		||||
    chmod +x $CODER_DIR/slackme
 | 
			
		||||
    EOF 
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,28 @@
 | 
			
		||||
#!/usr/bin/env node
 | 
			
		||||
 | 
			
		||||
PROVIDER_ID=${PROVIDER_ID}
 | 
			
		||||
 | 
			
		||||
BOT_TOKEN=$(coder external-auth access-token $PROVIDER_ID)
 | 
			
		||||
 | 
			
		||||
if [ $? -ne 0 ]; then
 | 
			
		||||
  echo "Authenticate to run commands in the background:"
 | 
			
		||||
  # The output contains the URL if failed.
 | 
			
		||||
  echo $BOT_TOKEN
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
USER_ID=$(coder external-auth access-token $PROVIDER_ID --extra "authed_user.id")
 | 
			
		||||
 | 
			
		||||
if [ $? -ne 0 ]; then
 | 
			
		||||
  echo "Failed to get authenticated user ID:"
 | 
			
		||||
  echo $USER_ID
 | 
			
		||||
  exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
echo "We'll notify you when done!"
 | 
			
		||||
 | 
			
		||||
# Run all arguments as a command
 | 
			
		||||
$@
 | 
			
		||||
 | 
			
		||||
curl --silent -o /dev/null --header "Authorization: Bearer $BOT_TOKEN" \
 | 
			
		||||
    "https://slack.com/api/chat.postMessage?channel=$USER_ID&text=Your%20command%20finished!&pretty=1"
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue