Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions kratos.gid/apps/MdpaGenerator/start.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ proc ::MdpaGenerator::BreakRunCalculation {} {
proc write::GetWriteMode {} {
return [::MdpaGenerator::xml::GetCurrentWriteMode]
}


proc ::MdpaGenerator::CustomToolbarItems { } {
variable dir

Kratos::ToolbarAddItem "OpenFlowgraph" "graph.png" [list -np- Flowgraph::LaunchFlowgraph] [= "Open Flowgraph"]
}
94 changes: 94 additions & 0 deletions kratos.gid/exec/docker_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# import tohil

# tcl=tohil.import_tcl()
Comment thread
jginternational marked this conversation as resolved.
Outdated
import subprocess

if __name__ == "__main__":
# debug stand alone to find meshio in the plugin path, instead site-packages
import os
plugin_site_packages=os.path.join(os.path.dirname(os.path.abspath(__file__)), "site-packages","src")
import sys
sys.path.append(plugin_site_packages)

def myfunction():
return "65"

Comment thread
jginternational marked this conversation as resolved.
Outdated
def isDockerAvailable():
# if docker is installed and available
try:
# execute "docker --version" and return the output
result1 = subprocess.run(["docker", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = result1.stdout.decode().strip()
return result
except (subprocess.CalledProcessError, FileNotFoundError):
return -1
return -1

def isDockerRunning():
# if docker is running
try:
result1 = subprocess.run(["docker", "info"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# get the output and check if it contains "Server Version"
result = result1.stdout.decode().strip()
return result
except (subprocess.CalledProcessError, FileNotFoundError):
return -1
return -1

# is docker running any container for an image
def isDockerRunningContainer(image_name, external_port=-1):
Comment thread
jginternational marked this conversation as resolved.
Outdated
try:
result = subprocess.run(["docker", "ps", "--filter", f"ancestor={image_name}","--filter", f"publish={external_port}" if external_port != -1 else "", "--format", "{{.ID}}"],
Comment thread
jginternational marked this conversation as resolved.
Outdated
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
container_ids = result.stdout.decode().strip().split('\n')
# tcl.W(container_ids)
Comment thread
jginternational marked this conversation as resolved.
Outdated
if container_ids == ['']:
return 0
# tcl.W(len(container_ids))
Comment thread
jginternational marked this conversation as resolved.
Outdated
return len(container_ids)
except (subprocess.CalledProcessError, FileNotFoundError):
return False
return False

Comment thread
jginternational marked this conversation as resolved.
Outdated
def killContainersFromImage(image_name, external_port=-1):
try:
result = subprocess.run(
["docker", "ps", "-q", "--filter", f"ancestor={image_name}", "--filter", f"publish={external_port}" if external_port != -1 else "", "--format", "{{.ID}}"],
Comment thread
jginternational marked this conversation as resolved.
Outdated
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
container_ids = result.stdout.strip().splitlines()

if not container_ids:
return 0

result = subprocess.run(["docker", "rm", "-f"] + container_ids, check=True)
return result
Comment thread
jginternational marked this conversation as resolved.
Outdated

except subprocess.CalledProcessError as e:
return -1 # Error al ejecutar docker

def startContainerForImage(image_name, external_port, internal_port, modelname):
try:
result = subprocess.run(
["docker", "run", "-d", "-p", f"{external_port}:{internal_port}", "-v", f"{modelname}:/model", image_name],
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
container_id = result.stdout.strip()
return container_id

except subprocess.CalledProcessError as e:
return -1 # Error al ejecutar docker

# -np- GiD_Python_Source C:/Users/jgarate/Desktop/CODE/Other/GiDInterface/kratos.gid/exec/check_docker.py
# check_docker.check_docker()
# -np- GiD_Python_Exec check_docker.check_docker()

# -np- GiD_Python_Import_File C:/Users/jgarate/Desktop/CODE/Other/GiDInterface/kratos.gid/exec/check_docker.py
# -np- GiD_Python_Exec check_docker.check_docker()

# print(isDockerAvailable())
# print(isDockerRunning())
# print(isDockerRunningContainer("flowgraph"))
# print(killContainersFromImage("flowgraph"))
# print(killContainersFromImage("flowgraph", 8080))
# print(startContainerForImage("flowgraph", 8080, 80, "C:\\Users\\jgarate\\Desktop\\bbb.gid")) # Adjust the model path as needed
Comment thread
jginternational marked this conversation as resolved.
Outdated
Binary file added kratos.gid/images/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion kratos.gid/kratos.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ proc Kratos::LoadCommonScripts { } {
uplevel #0 [list source [file join $kratos_private(Path) scripts Model $filename.tcl]]
}
# Libs
foreach filename {SimpleXMLViewer} {
foreach filename {SimpleXMLViewer flowgraph-launcher} {
uplevel #0 [list source [file join $kratos_private(Path) libs $filename.tcl]]
}
}
Expand Down
146 changes: 146 additions & 0 deletions kratos.gid/libs/flowgraph-launcher.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#########################################################
################### Javi Garate #########################
###################### CIMNE ############################
#########################################################

# if namespace exists, destroy it
if {[namespace exists ::Flowgraph]} {
namespace delete ::Flowgraph
}
namespace eval Flowgraph {
Kratos::AddNamespace [namespace current]

variable docker_image
variable flowgraph_internal_port
set flowgraph_internal_port 8182
variable flowgraph_external_port
set flowgraph_external_port 8182

variable mode

}

proc Flowgraph::Init { } {
variable docker_image
set docker_image "flowgraph"

variable mode
set mode [Kratos::ManagePreferences GetValue flowgraph_mode]
if {$mode eq ""} {
set mode "local"
Kratos::ManagePreferences SetValue flowgraph_mode $mode
}
# W "Flowgraph launcher initialized for image: $docker_image"
}


proc Flowgraph::LaunchFlowgraph {} {
variable mode
set mode [Kratos::ManagePreferences GetValue flowgraph_mode]

variable flowgraph_external_port
set flowgraph_external_port [Kratos::ManagePreferences GetValue flowgraph_external_port]

if {$mode eq "docker"} {
Flowgraph::LaunchFlowgraphDocker
} else {
Flowgraph::LaunchFlowgraphLocal
}

}


####################################################
################ PRIVATE METHODS ###################
####################################################


proc Flowgraph::LaunchFlowgraphLocal {} {
variable flowgraph_external_port

VisitWeb "http://localhost:$flowgraph_external_port"
}

proc Flowgraph::LaunchFlowgraphDocker {} {
variable docker_image
variable flowgraph_internal_port
variable flowgraph_external_port

set modelname [GiD_Info Project ModelName]
if {$modelname eq "UNNAMED"} {
W "Please save the model before launching Flowgraph"
return ""
}
set modelname $modelname.gid
set status [Flowgraph::IsFlowgraphEnabled]
switch $status {
-2 {
W "Docker is not installed. Please install Docker from https://www.docker.com/get-started"
}
-1 {
W "Docker is not available. Make sure Docker is running."
}
0 {
W "Flowgraph is already running. Stop the current instance to start a new one."
W "You can do it by executing in a console: docker rm -f \$(docker ps -q --filter 'ancestor=$docker_image' --filter 'publish=$flowgraph_external_port')"
W "Or copy-paste this command in the GiD Terminal:"
W "-np- Flowgraph::KillContainer"
Comment thread
jginternational marked this conversation as resolved.
}
default {
W "Starting Flowgraph... We are going to execute `docker run --rm -p $flowgraph_external_port:$flowgraph_internal_port -v $modelname:/model $docker_image`"
# Code to start Flowgraph
W "Starting..."
Kratos::StartContainerForImage $docker_image $flowgraph_external_port $flowgraph_internal_port $modelname
# exec docker run --rm -p $flowgraph_external_port:$flowgraph_internal_port -v $modelname:/model $docker_image &
W "Flowgraph started. Open your browser and go to http://localhost:$flowgraph_external_port"
VisitWeb "http://localhost:$flowgraph_external_port"
W "To stop Flowgraph, copy-paste this command in the GiD Terminal:"
W "-np- Flowgraph::KillContainer"

}
}
}

# Will return -2 if docker is not installed
# Will return -1 if docker is not available
# Will return 1 if docker is available and no instance of flowgraph is running
# Will return 0 if docker is available and an instance of flowgraph is running

proc Flowgraph::IsFlowgraphEnabled {} {
variable docker_image
set docker_status [Kratos::IsDockerAvailable]

W "Docker status: $docker_status"
Comment thread
jginternational marked this conversation as resolved.

if {$docker_status == -1} {
return -1
} else {

set running [::Kratos::IsDockerContainerRunningForImage $docker_image]
W "Docker image $docker_image is available: running=$running"
Comment thread
jginternational marked this conversation as resolved.
if {$running} {
return 0
} else {
return 1
}
}
}

proc Flowgraph::KillContainer {} {
variable docker_image
variable flowgraph_external_port
W "Stopping all running instances of $docker_image ..."
set result [Kratos::KillAllContainersForImage $docker_image $flowgraph_external_port]
if {$result ne -1} {
W "Stopped $result instances of $docker_image"
} else {
W "Error stopping instances of $docker_image"
}
}

Flowgraph::Init

# print the list of methods od the namespace
# W [namespace children Flowgraph]

# -np- source {C:\Users\jgarate\Desktop\CODE\Other\GiDInterface\kratos.gid\libs\flowgraph-launcher.tcl}
73 changes: 43 additions & 30 deletions kratos.gid/scripts/Controllers/Preferences.xml
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
<group name='kratos_preferences' label='Kratos'>
<labelframe name='main_options' label='General options'>
<combobox name='DevMode' variable='DevMode' variablemanager='Kratos::ManagePreferences' label='Mode' help='Show/Hide some experimental features'>
<option value='dev' label='Developer'/>
<option value='release' label='Release'/>
</combobox>
<combobox name='mdpa_format' variable='mdpa_format' variablemanager='Kratos::ManagePreferences' label='MDPA Optimization' help=''>
<option value='0' label='Optimized'/>
<option value='1' label='Readable'/>
</combobox>
<combobox name='allow_logs' variable='allow_logs' variablemanager='Kratos::ManagePreferences' label='Store logs' help=''>
<option value='0' label='No'/>
<option value='1' label='Only local, so I will get better support from the developers.'/>
<option value='2' label='Share anonymous usage data. (See kratos > View current log) Coming soon :)'/>
</combobox>
<combobox name='echo_level' variable='echo_level' variablemanager='Kratos::ManagePreferences' label='Echo level' help='Show more/less message'>
<option value='0' label='0'/>
<option value='1' label='1'/>
<option value='2' label='2'/>
</combobox>
<entrywithbutton name="debug_folder" label="Debug path" variable="debug_folder" buttonimage="folder.png" variablemanager='Kratos::ManagePreferences' buttonfunction="OpenBrowserForDirectoryDebug" help='Path to the kratos debug folder. This is placed in the launch.json file for debugging' />
<comboboxframe name="launch_configuration" label="Launch configuration" variable="launch_configuration" variablemanager='Kratos::ManagePreferences' help='' >
<option value='Default' label='Default'/>
<option value='External python' label='External python' setactivate="python_path"/>
<option value='Your compiled Kratos' label='Your compiled Kratos' setactivate="python_path kratos_bin_path"/>
<option value='Docker' label='Docker' setactivate="docker_image" />
<entrywithbutton name="python_path" variable="python_path" label="Python path (exe)" variablemanager='Kratos::ManagePreferences' help='Path to python.exe' buttonimage="folder.png" buttonfunction="OpenBrowserForDirectoryPython"/>
<entrywithbutton name="kratos_bin_path" variable="kratos_bin_path" label="Kratos path (bin)" variablemanager='Kratos::ManagePreferences' help='Path to your compiled kratos folder' buttonimage="folder.png" buttonfunction="OpenBrowserForDirectoryKratos"/>
<entry name="docker_image" variable="docker_image" label="Docker image" variablemanager='Kratos::ManagePreferences' help='Name of the Kratos Docker image'/>
</comboboxframe>
</labelframe>
<group name='main_options_group' label='General options'>
Comment thread
jginternational marked this conversation as resolved.
<labelframe name='main_options' label='General options'>
<combobox name='DevMode' variable='DevMode' variablemanager='Kratos::ManagePreferences' label='Mode' help='Show/Hide some experimental features'>
<option value='dev' label='Developer'/>
<option value='release' label='Release'/>
</combobox>
<combobox name='mdpa_format' variable='mdpa_format' variablemanager='Kratos::ManagePreferences' label='MDPA Optimization' help=''>
<option value='0' label='Optimized'/>
<option value='1' label='Readable'/>
</combobox>
<combobox name='allow_logs' variable='allow_logs' variablemanager='Kratos::ManagePreferences' label='Store logs' help=''>
<option value='0' label='No'/>
<option value='1' label='Only local, so I will get better support from the developers.'/>
<option value='2' label='Share anonymous usage data. (See kratos > View current log) Coming soon :)'/>
</combobox>
<combobox name='echo_level' variable='echo_level' variablemanager='Kratos::ManagePreferences' label='Echo level' help='Show more/less message'>
<option value='0' label='0'/>
<option value='1' label='1'/>
<option value='2' label='2'/>
</combobox>
<entrywithbutton name="debug_folder" label="Debug path" variable="debug_folder" buttonimage="folder.png" variablemanager='Kratos::ManagePreferences' buttonfunction="OpenBrowserForDirectoryDebug" help='Path to the kratos debug folder. This is placed in the launch.json file for debugging' />
<comboboxframe name="launch_configuration" label="Launch configuration" variable="launch_configuration" variablemanager='Kratos::ManagePreferences' help='' >
<option value='Default' label='Default'/>
<option value='External python' label='External python' setactivate="python_path"/>
<option value='Your compiled Kratos' label='Your compiled Kratos' setactivate="python_path kratos_bin_path"/>
<option value='Docker' label='Docker' setactivate="docker_image" />
<entrywithbutton name="python_path" variable="python_path" label="Python path (exe)" variablemanager='Kratos::ManagePreferences' help='Path to python.exe' buttonimage="folder.png" buttonfunction="OpenBrowserForDirectoryPython"/>
<entrywithbutton name="kratos_bin_path" variable="kratos_bin_path" label="Kratos path (bin)" variablemanager='Kratos::ManagePreferences' help='Path to your compiled kratos folder' buttonimage="folder.png" buttonfunction="OpenBrowserForDirectoryKratos"/>
<entry name="docker_image" variable="docker_image" label="Docker image" variablemanager='Kratos::ManagePreferences' help='Name of the Kratos Docker image'/>
</comboboxframe>
</labelframe>
</group>
<!-- another group for flowgraph -->
<group name='flowgraph_options' label='Flowgraph options'>
<labelframe name='flowgraph_options' label='Flowgraph options'>
<comboboxframe name='flowgraph_mode' variable='flowgraph_mode' variablemanager='Kratos::ManagePreferences' label='Mode' help='Choose the mode for the flowgraph'>
<option value='docker' label='Docker' setactivate="flowgraph_docker_image flowgraph_external_port"/>
<option value='local' label='Local' setactivate="flowgraph_external_port"/>
<entry name="flowgraph_docker_image" variable="flowgraph_docker_image" label="Docker image" variablemanager='Kratos::ManagePreferences' help='Name of the Flowgraph Docker image'/>
<entry name="flowgraph_external_port" variable="flowgraph_external_port" label="Port" variablemanager='Kratos::ManagePreferences' help='Port to access Flowgraph from the browser'/>
</comboboxframe>
</labelframe>
</group>
</group>

10 changes: 10 additions & 0 deletions kratos.gid/scripts/Controllers/PreferencesWindow.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ proc Kratos::ManagePreferences { cmd name {value ""}} {
"kratos_bin_path" {
set ret [file join $::Kratos::kratos_private(Path) exec kratos runkratos.exe]
}
"flowgraph_docker_image" {
set ret "flowgraph"
}
"flowgraph_external_port" {
set ret 8182
}
"flowgraph_mode" {
set ret "docker"
}

default {
set ret 0
}
Expand Down
Loading