github.com/hernad/nomad@v1.6.112/ui/app/services/sockets.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import Service from '@ember/service'; 7 import config from 'nomad-ui/config/environment'; 8 import { getOwner } from '@ember/application'; 9 import { inject as service } from '@ember/service'; 10 11 export default class SocketsService extends Service { 12 @service system; 13 14 getTaskStateSocket(taskState, command) { 15 const mirageEnabled = 16 config.environment !== 'production' && 17 config['ember-cli-mirage'] && 18 config['ember-cli-mirage'].enabled !== false; 19 20 if (mirageEnabled) { 21 return new Object({ 22 messageDisplayed: false, 23 24 send(e) { 25 if (!this.messageDisplayed) { 26 this.messageDisplayed = true; 27 this.onmessage({ 28 data: `{"stdout":{"data":"${btoa( 29 'unsupported in Mirage\n\r' 30 )}"}}`, 31 }); 32 } else { 33 this.onmessage({ data: e.replace('stdin', 'stdout') }); 34 } 35 }, 36 }); 37 } else { 38 const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; 39 const applicationAdapter = getOwner(this).lookup('adapter:application'); 40 const prefix = `${ 41 applicationAdapter.host || window.location.host 42 }/${applicationAdapter.urlPrefix()}`; 43 const region = this.system.activeRegion; 44 45 return new WebSocket( 46 `${protocol}//${prefix}/client/allocation/${taskState.allocation.id}` + 47 `/exec?task=${taskState.name}&tty=true&ws_handshake=true` + 48 (region ? `®ion=${region}` : '') + 49 `&command=${encodeURIComponent(`["${command}"]`)}` 50 ); 51 } 52 } 53 }