import Ember from 'ember'; import C from 'ui/utils/constants'; import Util from 'ui/utils/util'; import { denormalizeId, denormalizeIdArray } from 'ember-api-store/utils/denormalize'; import Instance from 'ui/models/instance'; import { formatSi } from 'ui/utils/util'; var Container = Instance.extend({ // Common to all instances requestedHostId : null, primaryIpAddress : null, primaryAssociatedIpAddress : null, projects : Ember.inject.service(), modalService: Ember.inject.service('modal'), // Container-specific type : 'container', imageUuid : null, registryCredentialId : null, command : null, commandArgs : null, environment : null, ports : null, instanceLinks : null, dataVolumes : null, dataVolumesFrom : null, devices : null, restartPolicy : null, mounts : denormalizeIdArray('mountIds'), primaryHost : denormalizeId('hostId'), services : denormalizeIdArray('serviceIds'), primaryService : Ember.computed.alias('services.firstObject'), primaryStack : Ember.computed.alias('primaryService.stack'), referencedStack : denormalizeId('stackId'), referencedService : denormalizeId('serviceId'), service: Ember.computed('primaryService','referencedService', function() { return this.get('referencedService') || this.get('primaryService'); }), stack: Ember.computed('primaryStack','referencedStack', function() { return this.get('referencedStack') || this.get('primaryStack'); }), actions: { restart: function() { return this.doAction('restart'); }, start: function() { return this.doAction('start'); }, promptStop: function() { this.get('modalService').toggleModal('modal-container-stop', { model: [this] }); }, stop: function() { this.doAction('stop'); }, shell: function() { this.get('modalService').toggleModal('modal-shell', { model: this, escToClose: false, }); }, popoutShell: function() { let proj = this.get('projects.current.id'); let id = this.get('id'); Ember.run.later(() => { window.open(`//${window.location.host}/env/${proj}/infra/console?instanceId=${id}&isPopup=true`, '_blank', "toolbars=0,width=900,height=700,left=200,top=200"); }); }, popoutLogs: function() { let proj = this.get('projects.current.id'); let id = this.get('id'); Ember.run.later(() => { window.open(`//${window.location.host}/env/${proj}/infra/container-log?instanceId=${id}&isPopup=true`, '_blank', "toolbars=0,width=700,height=715,left=200,top=200"); }); }, logs: function() { this.get('modalService').toggleModal('modal-container-logs', this); }, edit: function() { this.get('router').transitionTo('containers.new', {queryParams: {containerId: this.get('id'), upgrade: true}}); }, clone: function() { this.get('router').transitionTo('containers.new', {queryParams: {containerId: this.get('id')}}); }, convertToService: function() { this.get('modalService').toggleModal('modal-container-to-service', this); }, }, availableActions: function() { var a = this.get('actionLinks'); if ( !a ) { return []; } var labelKeys = Object.keys(this.get('labels')||{}); var isSystem = this.get('isSystem'); var isService = labelKeys.indexOf(C.LABEL.SERVICE_NAME) >= 0; var isK8s = labelKeys.indexOf(C.LABEL.K8S_POD_NAME) >= 0; var canConvert = !!a.converttoservice && !isSystem && !isService && !isK8s; var choices = [ { label: 'action.upgradeOrEdit', icon: 'icon icon-edit', action: 'edit', enabled: !!a.update && !isK8s }, { label: 'action.convertToService', icon: 'icon icon-service', action: 'convertToService', enabled: canConvert}, { label: 'action.clone', icon: 'icon icon-copy', action: 'clone', enabled: !isSystem && !isService && !isK8s}, { divider: true }, { label: 'action.execute', icon: 'icon icon-terminal', action: 'shell', enabled: !!a.execute, altAction:'popoutShell'}, { label: 'action.console', icon: 'icon icon-terminal', action: 'console', enabled: !!a.console, altAction:'popoutShellVm' }, { label: 'action.logs', icon: 'icon icon-file', action: 'logs', enabled: !!a.logs, altAction: 'popoutLogs' }, { divider: true }, { label: 'action.restart', icon: 'icon icon-refresh', action: 'restart', enabled: !!a.restart, bulkable: true}, { label: 'action.start', icon: 'icon icon-play', action: 'start', enabled: !!a.start, bulkable: true}, { label: 'action.stop', icon: 'icon icon-stop', action: 'promptStop', enabled: !!a.stop, altAction: 'stop', bulkable: true}, { divider: true }, { label: 'action.remove', icon: 'icon icon-trash', action: 'promptDelete', enabled: this.get('canDelete'), altAction: 'delete', bulkable: true}, { divider: true }, { label: 'action.viewInApi', icon: 'icon icon-external-link',action: 'goToApi', enabled: true }, ]; return choices; }.property('actionLinks.{restart,start,stop,restore,execute,logs,update,converttoservice}','canDelete','isSystem'), memoryReservationBlurb: Ember.computed('memoryReservation', function() { if ( this.get('memoryReservation') ) { return formatSi(this.get('memoryReservation'), 1024, 'iB', 'B'); } }), combinedState: function() { var resource = this.get('state'); var health = this.get('healthState'); var hasCheck = !!this.get('healthCheck'); if ( C.ACTIVEISH_STATES.indexOf(resource) >= 0 ) { if ( hasCheck && health ) { return health; } else { return resource; } } else if ((resource === 'stopped') && ((this.get('labels')||{})[C.LABEL.START_ONCE]) && (this.get('startCount') > 0)) { return 'started-once'; } else { return resource; } }.property('state', 'healthState'), isOn: function() { return ['running','updating-running','migrating','restarting'].indexOf(this.get('state')) >= 0; }.property('state'), displayIp: function() { return this.get('primaryAssociatedIpAddress') || this.get('primaryIpAddress') || null; }.property('primaryIpAddress','primaryAssociatedIpAddress'), sortIp: function() { var ip = this.get('primaryAssociatedIpAddress') || this.get('primaryIpAddress'); if ( !ip ) { return ''; } var match = ip.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); if ( match ) { return match.slice(1).map((octet) => { return Util.strPad(octet,3,'0',false); }).join("."); } }.property('primaryIpAddress','primaryAssociatedIpAddress'), canDelete: function() { return ['removed','removing','purging','purged'].indexOf(this.get('state')) === -1; }.property('state'), isSystem: function() { var labelKeys = Object.keys(this.get('labels')||{}); var isSystem = !!this.get('system') || labelKeys.indexOf(C.LABEL.SYSTEM_TYPE) >= 0; return isSystem; }.property('system','labels'), displayImage: function() { return (this.get('imageUuid')||'').replace(/^docker:/,''); }.property('imageUuid'), displayExternalId: function() { var id = this.get('externalId'); if ( id ) { return (Ember.Handlebars.Utils.escapeExpression(id.substr(0,6))+"…").htmlSafe(); } }.property('externalId'), isGlobalScale: function() { return (this.get('labels')||{})[C.LABEL.SCHED_GLOBAL] + '' === 'true'; }.property('labels'), }); export default Container;